1. 程式人生 > >Mongodb-使用C#Drivers實現增刪改查

Mongodb-使用C#Drivers實現增刪改查

<pre name="code" class="csharp">using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MongoDBDemo1
{
   public class Connection
    {
         //建立資料庫連線
       MongoServerSettings setting = new MongoServerSettings();
       MongoServerAddress address =new MongoServerAddress("127.0.0.1", 27017);

       MongoServer server = null;
       string dbName=string.Empty;

       /// <summary>
       ///  得到連線
       /// </summary>
	  public Connection()
       {
           setting.Server = address;
           server = new MongoServer(setting);
       }

        /// <summary>
        /// 建構函式  獲得 dbname
        /// </summary>
        /// <param name="dbname">資料庫名稱</param>
        public Connection(string dbname)
       {
           this.dbName = dbname;
           setting.Server = address;
           server = new MongoServer(setting);
       } 



		  /// <summary>
       /// 外部呼叫得到資料庫
       /// </summary>
       /// <param name="dbName"></param>
       /// <returns></returns>
       public MongoDatabase getMongoDataBase(string dbName)
       {
           return server.GetDatabase(dbName);
       
       }
       /// <summary>
       /// 過載 得到資料庫
       /// </summary>
       /// <returns></returns>
       public MongoDatabase getMongoDataBase()
       {
           return server.GetDatabase(dbName);
       
       }

       /// <summary>
       /// 建立集合
       /// </summary>
       /// <param name="colName">集合名字</param>
       /// <returns></returns>
       public bool createCollection(string colName) 
       {
           MongoDatabase db = this.getMongoDataBase();
           CommandResult result= db.CreateCollection(colName);
           if (result.Ok)
           {
               return true;
           }
           else 
           {
               return false;
           }
       }

		
       /// <summary>
       /// 外部呼叫
       /// </summary>
       /// <param name="colName">集合名稱</param>
       /// <param name="db">資料庫物件</param>
       /// <returns></returns>

       public MongoCollection<BsonDocument> getMongoColl(string colName,MongoDatabase db){
          return db.GetCollection(colName);
       }

       /// <summary>
       /// 內部使用
       /// </summary>
       /// <param name="colName">集合名稱</param>
       /// <returns></returns>

       public MongoCollection<BsonDocument> getMongoColl(string colName){
          return this.getMongoDataBase().GetCollection(colName);
       }
 


        /// <summary>
       /// 查詢操作
        /// </summary>
        /// <param name="colName">集合名詞</param>
        /// <param name="query">條件</param>
        /// <returns></returns>
       public MongoCursor<BsonDocument> select(string colName,IMongoQuery query){

           MongoCollection<BsonDocument> mcoll=getMongoColl(colName);
           try
           {
               if (query == null)
               {
                   return mcoll.FindAll();
               }
               else
               {
                   return mcoll.Find(query);
               }
           }
           finally {
               this.mongoClose();
           }
           
       }


       /// <summary>
       /// 增加操作
       /// </summary>
       /// <param name="colName">集合名稱</param>
       /// <param name="bson">單條資料</param>
       /// <returns></returns>
       public bool insert(string colName,BsonDocument bson) 
       {
           MongoCollection<BsonDocument> mcoll = getMongoColl(colName);
           try
           {
               mcoll.Insert(bson);
               return true;
           }
           catch
           {
               return false;
           }
           finally {
               this.mongoClose();
           }
       }

       /// <summary>
       /// 增加多條
       /// </summary>
       /// <param name="colName">集合名稱</param>
       /// <param name="bsonlist">資料集合</param>
       /// <returns></returns>
       public bool insert(string colName, List<BsonDocument> bsonlist)
       {
           MongoCollection<BsonDocument> mcoll = getMongoColl(colName);
           try
           {
               foreach (BsonDocument bson in bsonlist)
               {
                   mcoll.Insert(bson);
               }
               return true;
           }
           catch
           {
               return false;
           }
           finally {
               this.mongoClose();
           
           }
       }

       /// <summary>
       /// 修改操作
       /// </summary>
       /// <param name="colName">集合名稱</param>
       /// <param name="query">條件</param>
       /// <param name="upd">修改的內容</param>
       /// <returns></returns>
       public bool update(string colName, IMongoQuery query,IMongoUpdate upd)
       {
           MongoCollection<BsonDocument> mcoll = getMongoColl(colName);
           try
           {
               mcoll.Update(query, upd);
               return true;
           }
           catch
           {
               return false;
           }
           finally {
               this.mongoClose();
           }
       }

       /// <summary>
       /// 刪除操作
       /// </summary>
       /// <param name="cloName">集合名稱</param>
       /// <param name="query">條件</param>
       /// <param name="flag">是否為0 ,為0 則刪除 滿足條件的全部資訊</param>
       /// <returns>false/true</returns>
       public bool delete(string cloName, IMongoQuery query,int flag) {
           MongoCollection<BsonDocument> mcol = this.getMongoColl(cloName);
           try
           {
               if (flag == 0)
               {
                   mcol.Remove(query, RemoveFlags.None);
               }
               else
               {
                   mcol.Remove(query, RemoveFlags.Single);
               }
               return true;
           }
           catch
           {
               return false;
           }
           finally {
               this.mongoClose();
           }
       }





       /// <summary>
       /// 關閉連線
       /// </summary>
       public void mongoClose(){
         server.Disconnect();
       }
    
   }

}