C# Mongodb基礎應用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;

namespace TalentCloud.Base.MongodbHelper
{
    public class MongodbHelper
    {

        public void Demo() {
            MongodbHelper MongodbHelper = new MongodbHelper();
            UsersInfo UsersInfo = new UsersInfo();
            UsersInfo.Name = "ads";
            UsersInfo.Sex = "男";
            MongodbHelper.Insert<UsersInfo>(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo", UsersInfo);
            List<UsersInfo> aa = MongodbHelper.Query<UsersInfo>(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo");

            MongodbHelper.Update(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo", MongodbHelper.NewFilter("Name", "ssn2"), MongodbHelper.NewUpdate("Name", "ssn3"));

            List<UsersInfo> aa2 = MongodbHelper.Query<UsersInfo>(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo", MongodbHelper.NewFilter("Name", "ssn2"));

            List<UsersInfo> aa1 = MongodbHelper.Query<UsersInfo>(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo");

            MongodbHelper.Delete(MongoEnum.MongoDbEnum.LoginUserInfo, "UsersInfo", MongodbHelper.NewFilter("Name", "ssn"));
        }
        
        /// <summary>
        /// 插入數據
        /// </summary>
        /// <typeparam name="T">泛型數據</typeparam>
        /// <param name="dbName">插入的數據庫</param>
        /// <param name="ObjName">插入的對象名,相似表名</param>
        /// <param name="data">數據對象</param>
        public void Insert<T>(string dbName, string ObjName, T data)
        {
            MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
            //得到Users集合,若是數據庫中沒有,先新建一個 
            MongoCollection MongoCollection = db.GetCollection(ObjName);
            //執行插入操做
            MongoCollection.Insert<T>(data);
        }

        /// <summary>
        /// 更新數據
        /// </summary>
        /// <param name="dbName"></param>
        /// <param name="ObjName"></param>
        /// <param name="query">    定義獲取「Name」值爲「xumingxiang」的查詢條件 var query = new QueryDocument { { "Name", "xumingxiang" } };</param>
        /// <param name="update">  定義更新文檔   var update = new UpdateDocument { { "$set", new QueryDocument { { "Sex", "wowen" } } } };</param>
        /// 注意:千萬要注意查詢條件和數據庫內的字段類型保持一致,不然將沒法查找到數據。如:若是Sex是整型,一點要記得將值轉換爲整型。
        public void Update(string dbName, string ObjName, QueryDocument query, UpdateDocument update)
        {
            MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
            //獲取Users集合 
            MongoCollection MongoCollection = db.GetCollection(ObjName);
            //執行更新操做 
            MongoCollection.Update(query, update);
        }

        /// <summary>
        /// 刪除數據
        /// </summary>
        /// <param name="dbName"></param>
        /// <param name="ObjName"></param>
        /// <param name="query"> 定義獲取「Name」值爲「xumingxiang」的查詢條件  var query = new QueryDocument { { "Name", "xumingxiang" } };</param>
        public void Delete(string dbName, string ObjName, QueryDocument query)
        {
            MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
            //獲取Users集合 
            MongoCollection MongoCollection = db.GetCollection(ObjName);
            //執行刪除操做 
            MongoCollection.Remove(query);
        }

        /// <summary>
        /// 查詢所有數據
        /// </summary>
        /// <param name="dbName">須要查詢的庫</param>
        public List<T> Query<T>(string dbName, string ObjName)
        {

            MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
            //獲取Users集合 
            MongoCollection col = db.GetCollection(ObjName);
            //查詢所有集合裏的數據 
            return col.FindAllAs<T>().ToList();
             
        }

        /// <summary>
        /// 查詢指定查詢條件的所有數據 
        /// </summary>
        /// <param name="dbName">須要查詢的庫</param>
        /// <param name="query">查詢條件 var query = new QueryDocument { { "Name", "xumingxiang" } };</param>
        public List<T> Query<T>(string dbName, string ObjName, QueryDocument query)
        {

            MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
            //獲取Users集合 
            MongoCollection col = db.GetCollection(ObjName);
            //查詢指定查詢條件的所有數據 
            return col.FindAs<T>(query).ToList(); ;
        }

        /// <summary>
        /// 查詢指定查詢條件的第一條數據
        /// </summary>
        /// <param name="dbName">須要查詢的庫</param>
        /// <param name="query">查詢條件 var query = new QueryDocument { { "Name", "xumingxiang" } };</param>
        public T QueryOne<T>(string dbName, string ObjName, QueryDocument query)
        {

            MongoDatabase db = MongodbUtil.GetMongodbDatabase(dbName);
            //獲取Users集合 
            MongoCollection col = db.GetCollection(ObjName);
            //查詢指定查詢條件的所有數據 
            return col.FindOneAs<T>(query); ;
        }

        /// <summary>
        /// 建立查詢條件
        /// </summary>
        /// <returns></returns>
        public QueryDocument NewFilter(string key,string value) {
            return new QueryDocument { { key, value } };
        }

        /// <summary>
        /// 建立更新條件
        /// </summary>
        /// <returns></returns>
        public UpdateDocument NewUpdate(string key, string value)
        {
            return new UpdateDocument { { "$set", new QueryDocument { { key, value } } } };
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Configuration;

namespace TalentCloud.Base.MongodbHelper
{
   public class MongodbUtil
    {

       public static string MongodbConnection
       {
           get
           {
         //mongodb://127.0.0.1:27017
return ConfigurationManager.AppSettings["MongodbConnection"].ToString(); } } /// <summary> /// 獲取MongoClient /// </summary> /// <returns></returns> public static MongoServer GetMongoServer() { //建立數據庫連接 return MongoServer.Create(MongodbConnection); } /// <summary> /// 獲取MongoServer /// </summary> /// <returns></returns> public static MongoServer GetMongoServer(string Connection) { //建立數據庫連接 return MongoServer.Create(Connection); } //獲取DbBase public static MongoDatabase GetMongodbDatabase(string dbName) { MongoServer MongoServer = GetMongoServer(); //得到數據庫cnblogs MongoDatabase DbBase = MongoServer.GetDatabase(dbName); return DbBase; } } }

支持.net4.0mongodb

https://files.cnblogs.com/files/yyyuguo/mongodbDll.zip  數據庫

相關文章
相關標籤/搜索