zoukankan      html  css  js  c++  java
  • MongoDB-C#驱动帮助

    查增改删

    链接字符串 MongoDB超管+(admin) 单独库用户不加

    static string mongoR = string.Format("mongodb://{0}(admin):{1}@{2}:{3}", "MongoRead", "123456", "127.0.0.1", 27017);

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using MongoDB.Bson;
      5 using MongoDB.Driver;
      6 
      7 namespace MongoTest
      8 {
      9     public class EMongoModel
     10     {
     11         /// <summary>
     12         /// 连接字符串
     13         /// </summary>
     14         public string ConnStr { get; set; }
     15         /// <summary>
     16         /// 数据库名称
     17         /// </summary>
     18         public string DBName { get; set; }
     19         /// <summary>
     20         /// 数据库表名称
     21         /// </summary>
     22         public string CollName { get; set; }
     23     }
     24     public class EMongo
     25     {
     26         #region 查询
     27         public static T Select<T>(EMongoModel mongoM, IMongoQuery query = null) where T : class,new()
     28         {
     29             T t = null;
     30             try
     31             {
     32                 MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
     33                 MongoDatabase db = server.GetDatabase(mongoM.DBName);
     34                 using (server.RequestStart(db))
     35                 {
     36                     MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
     37                     t = dbCollection.FindOneAs<T>(query);
     38                 }
     39             }
     40             catch (Exception ex)
     41             {
     42                 Console.WriteLine(ex.Message);
     43             }
     44             return t;
     45         }
     46         public static List<T> SelectList<T>(EMongoModel mongoM, IMongoQuery query = null, IMongoSortBy sort = null, params string[] fields) where T : class,new()
     47         {
     48             List<T> ls = null;
     49             try
     50             {
     51                 MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
     52                 MongoDatabase db = server.GetDatabase(mongoM.DBName);
     53                 MongoCursor<T> cursor = null;
     54                 using (server.RequestStart(db))
     55                 {
     56                     MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
     57                     cursor = dbCollection.FindAs<T>(query);
     58                     if (sort != null) { cursor.SetSortOrder(sort); }
     59                     if (fields != null) { cursor.SetFields(fields); }
     60                 }
     61                 ls = cursor.ToList<T>();
     62             }
     63             catch (Exception ex)
     64             {
     65                 Console.WriteLine(ex.Message);
     66             }
     67             return ls;
     68         }
     69         public static List<T> SelectListPage<T>(EMongoModel mongoM, int pageIndex, int pageSize, out long rowCount, IMongoQuery query = null, IMongoSortBy sort = null, params string[] fields) where T : class,new()
     70         {
     71             List<T> ls = null;
     72             rowCount = 0;
     73             try
     74             {
     75                 MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
     76                 MongoDatabase db = server.GetDatabase(mongoM.DBName);
     77                 MongoCursor<T> cursor = null;
     78                 int startIndex = (pageIndex - 1) * pageSize;
     79                 using (server.RequestStart(db))
     80                 {
     81                     MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
     82                     cursor = dbCollection.FindAs<T>(query);
     83                     cursor.SetSkip(startIndex).SetLimit(pageSize);
     84                     rowCount = cursor.Count();
     85                     if (sort != null) { cursor.SetSortOrder(sort); }
     86                     if (fields != null && fields.Length > 0) { cursor.SetFields(fields); }
     87                 }
     88                 ls = cursor.ToList<T>();
     89             }
     90             catch (Exception ex)
     91             {
     92                 Console.WriteLine(ex.Message);
     93             }
     94             return ls;
     95         }
     96         #endregion
     97 
     98         #region 添加
     99         public static bool Insert<T>(EMongoModel mongoM, T model) where T : class,new()
    100         {
    101             bool ret = false;
    102             try
    103             {
    104                 MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
    105                 MongoDatabase db = server.GetDatabase(mongoM.DBName);
    106                 using (server.RequestStart(db))
    107                 {
    108                     MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
    109                     ret = dbCollection.Insert(model).Ok;
    110                 }
    111             }
    112             catch (Exception ex)
    113             {
    114                 Console.WriteLine(ex.Message);
    115             }
    116             return ret;
    117         }
    118         public static bool Insert<T>(EMongoModel mongoM, List<T> modelLs) where T : class,new()
    119         {
    120             bool ret = false;
    121             try
    122             {
    123                 MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
    124                 MongoDatabase db = server.GetDatabase(mongoM.DBName);
    125                 IEnumerable<WriteConcernResult> result = null;
    126                 using (server.RequestStart(db))
    127                 {
    128                     MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
    129                     result = dbCollection.InsertBatch(modelLs);
    130                 }
    131                 ret = result.Where(x => x.Ok).Count() > 0;
    132             }
    133             catch (Exception ex)
    134             {
    135                 Console.WriteLine(ex.Message);
    136             }
    137             return ret;
    138         }
    139         #endregion
    140 
    141         #region 修改
    142         public static bool Update<T>(EMongoModel mongoM, T model) where T : class,new()
    143         {
    144             bool ret = false;
    145             try
    146             {
    147                 MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
    148                 MongoDatabase db = server.GetDatabase(mongoM.DBName);
    149                 using (server.RequestStart(db))
    150                 {
    151                     MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
    152                     ret = dbCollection.Save<T>(model).Ok;
    153                 }
    154             }
    155             catch (Exception ex)
    156             {
    157                 Console.WriteLine(ex.Message);
    158             }
    159             return ret;
    160         }
    161         public static bool Update(EMongoModel mongoM, IMongoUpdate update, IMongoQuery query = null)
    162         {
    163             bool ret = false;
    164             try
    165             {
    166                 MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
    167                 MongoDatabase db = server.GetDatabase(mongoM.DBName);
    168                 using (server.RequestStart(db))
    169                 {
    170                     MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
    171                     ret = dbCollection.Update(query, update, UpdateFlags.Multi).Ok;
    172                 }
    173             }
    174             catch (Exception ex)
    175             {
    176                 Console.WriteLine(ex.Message);
    177             }
    178             return ret;
    179         }
    180         #endregion
    181 
    182         #region 删除
    183         public static bool Delete(EMongoModel mongoM, IMongoQuery query)
    184         {
    185             bool ret = false;
    186             try
    187             {
    188                 MongoServer server = new MongoClient(mongoM.ConnStr).GetServer();
    189                 MongoDatabase db = server.GetDatabase(mongoM.DBName);
    190                 using (server.RequestStart(db))
    191                 {
    192                     MongoCollection<BsonDocument> dbCollection = db.GetCollection<BsonDocument>(mongoM.CollName);//获取指定的表集合
    193                     ret = dbCollection.Remove(query).Ok;
    194                 }
    195             }
    196             catch (Exception ex)
    197             {
    198                 Console.WriteLine(ex.Message);
    199             }
    200             return ret;
    201         }
    202         #endregion
    203     }
    204 }
    View Code
  • 相关阅读:
    化了妆的祝福 4
    桥牌感悟 2
    关于送礼
    东京disney sea流水账 1
    东京disney sea流水账 3
    晕倒的候任日本驻华大使
    一饮一啄
    就算神游 之二:行路 4
    关于送礼续
    桥牌感悟 3
  • 原文地址:https://www.cnblogs.com/liuph/p/4519462.html
Copyright © 2011-2022 走看看