问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

.net2.0是否支持 mongodb

发布网友 发布时间:2022-05-02 01:15

我来回答

2个回答

懂视网 时间:2022-05-02 05:36

class MongoDbManager { private static IMongoDatabase db = null; private static readonly object locker = new object(); /// <summary> /// 使用单列模式创建连接 /// </summary> /// <returns></returns> public static IMongoDatabase CreateDb() { if (db == null) { lock (locker) { if (db == null) { MongoClient Client = new MongoClient(MongoDbConfig.Host); db = Client.GetDatabase(MongoDbConfig.DataBase); } } } return db; } }

第二步创建DB:

public class MongoDbBase
 {
 private IMongoDatabase db = null;
 public IMongoDatabase Db { get => db; }
 public MongoDbBase()
 {
  db = MongoDbManager.CreateDb();
 }
 }

最后献上自己mongodbhelper:

using KuRuMi.Mio.DoMain.MongoDbCache.MongoDbCommon;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace KuRuMi.Mio.DoMain.MongoDbCache.MongoDbCache
{
 /// <summary>
 /// MongoDb缓存
 /// </summary>
 public sealed class MongoDbCacheService : MongoDbBase
 {
 #region 同步
 #region 增加
 /// <summary>
 /// 保存单个对象
 /// </summary>
 /// <param name="Root"></param>
 /// <returns></returns>
 public bool AddSignleObject<TAggregateRoot>(TAggregateRoot Root)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  collection.InsertOne(Root);
  return true;
  }
  catch (Exception)
  {
  return false;
  }

 }
 /// <summary>
 /// 批量保存多个对象
 /// </summary>
 /// <param name="Root"></param>
 /// <returns></returns>
 public bool AddManyObject<TAggregateRoot>(List<TAggregateRoot> Root)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  collection.InsertMany(Root);
  return true;
  }
  catch (Exception)
  {
  return false;
  }

 }
 #endregion

 #region 删除
 /// <summary>
 /// 删除单个记录
 /// </summary>
 /// <typeparam name="TAggregateRoot"></typeparam>
 /// <param name="parameter"></param>
 /// <returns></returns>
 public int DeleteSingleIndex<TAggregateRoot>(Expression<Func<TAggregateRoot, bool>> filter)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  return (int)collection.DeleteOne(filter).DeletedCount;
  }
  catch (Exception e)
  {
  throw e;
  }
 }
 #endregion

 #region 查询
 /// <summary>
 /// 查询单条记录
 /// </summary>
 /// <typeparam name="TAggregateRoot"></typeparam>
 /// <param name="parameter"></param>
 /// <returns></returns>
 public TAggregateRoot FindSingleIndex<TAggregateRoot>(Expression<Func<TAggregateRoot, bool>> filter)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  return collection.Find(filter).FirstOrDefault();
  }
  catch (Exception e)
  {
  throw e;
  }
 }
 /// <summary>
 /// 查询整个集合
 /// </summary>
 /// <typeparam name="TAggregateRoot"></typeparam>
 /// <param name="filter"></param>
 /// <returns></returns>
 public List<TAggregateRoot> FindMany<TAggregateRoot>(Expression<Func<TAggregateRoot, bool>> filter)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  return collection.Find(filter).ToList();
  }
  catch (Exception e)
  {
  throw e;
  }
 }
 #endregion

 #region 更新
 /// <summary>
 /// 更新单个值
 /// </summary>
 /// <typeparam name="TAggregateRoot"></typeparam>
 /// <param name="filter"></param>
 /// <param name="field"></param>
 /// <param name="parameter"></param>
 /// <returns></returns>
 public int UpdateSingle<TAggregateRoot>(Expression<Func<TAggregateRoot, bool>> filter, string name, string parameter)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  var set = Builders<TAggregateRoot>.Update.Set(name, parameter);
  return (int)collection.UpdateOne(filter, set).ModifiedCount;
  }
  catch (Exception e)
  {
  throw e;
  }
 }
 /// <summary>
 /// 更新多个值
 /// </summary>
 /// <typeparam name="TAggregateRoot"></typeparam>
 /// <param name="filter"></param>
 /// <param name="Root"></param>
 /// <param name="property"></param>
 /// <param name="replace"></param>
 public int UpdateMany<TAggregateRoot>(Expression<Func<TAggregateRoot, bool>> filter, TAggregateRoot Root, List<string> property = null, bool replace = false)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  var type = Root.GetType();
  //修改集合
  var list = new List<UpdateDefinition<TAggregateRoot>>();
  foreach (var propert in type.GetProperties())
  {
   if (propert.Name.ToLower() != "id")
   {
   try
   {
    if (property == null && property.Count < 1 || property.Any(o => o.ToLower() == propert.Name.ToLower()))
    {
    var replaceValue = propert.GetValue(Root);
    if (replaceValue != null)
     list.Add(Builders<TAggregateRoot>.Update.Set(propert.Name, replaceValue));
    else if (replace)
     list.Add(Builders<TAggregateRoot>.Update.Set(propert.Name, replaceValue));
    }
   }
   catch (Exception)
   {
    if (property == null)
    {
    var replaceValue = propert.GetValue(Root);
    if (replaceValue != null)
     list.Add(Builders<TAggregateRoot>.Update.Set(propert.Name, replaceValue));
    else if (replace)
     list.Add(Builders<TAggregateRoot>.Update.Set(propert.Name, replaceValue));
    }
   }
   
   }
  }
  if (list.Count > 0)
  {
   var builders = Builders<TAggregateRoot>.Update.Combine(list);
   return (int)collection.UpdateOne(filter, builders).ModifiedCount;
  }
  return 0;
  }
  catch (Exception e)
  {
  throw e;
  }
 }
 #endregion
 #endregion

 #region 异步
 #region 增加
 /// <summary>
 /// 异步保存单个对象
 /// </summary>
 /// <param name="Root"></param>
 /// <returns></returns>
 public bool AddSignleObjectAsync<TAggregateRoot>(TAggregateRoot Root)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  collection.InsertOneAsync(Root);
  return true;
  }
  catch (Exception)
  {
  return false;
  }

 }
 /// <summary>
 /// 批量保存多个对象
 /// </summary>
 /// <param name="Root"></param>
 /// <returns></returns>
 public bool AddManyObjectAsync<TAggregateRoot>(List<TAggregateRoot> Root)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  collection.InsertManyAsync(Root);
  return true;
  }
  catch (Exception)
  {
  return false;
  }

 }
 #endregion

 #region 删除
 /// <summary>
 /// 异步删除单个记录
 /// </summary>
 /// <typeparam name="TAggregateRoot"></typeparam>
 /// <param name="parameter"></param>
 /// <returns></returns>
 public int DeleteSingleIndexAsync<TAggregateRoot>(Expression<Func<TAggregateRoot, bool>> filter)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  return (int)collection.DeleteOneAsync(filter).Result.DeletedCount;
  }
  catch (Exception e)
  {
  throw e;
  }
 }
 #endregion

 #region 查询
 /// <summary>
 /// 异步查询单条记录
 /// </summary>
 /// <typeparam name="TAggregateRoot"></typeparam>
 /// <param name="parameter"></param>
 /// <returns></returns>
 public TAggregateRoot FindSingleIndexAsync<TAggregateRoot>(Expression<Func<TAggregateRoot, bool>> filter)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  return collection.FindAsync(filter).Result.FirstOrDefault();
  }
  catch (Exception e)
  {
  throw e;
  }
 }
 /// <summary>
 /// 异步查询整个集合
 /// </summary>
 /// <typeparam name="TAggregateRoot"></typeparam>
 /// <param name="filter"></param>
 /// <returns></returns>
 public List<TAggregateRoot> FindManyAsync<TAggregateRoot>(Expression<Func<TAggregateRoot, bool>> filter)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  return collection.FindAsync(filter).Result.ToList();
  }
  catch (Exception e)
  {
  throw e;
  }
 }
 #endregion

 #region 更新
 /// <summary>
 /// 异步更新单个值
 /// </summary>
 /// <typeparam name="TAggregateRoot"></typeparam>
 /// <param name="filter"></param>
 /// <param name="field"></param>
 /// <param name="parameter"></param>
 /// <returns></returns>
 public int UpdateSingleAsync<TAggregateRoot>(Expression<Func<TAggregateRoot, bool>> filter, string name, string parameter)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  var set = Builders<TAggregateRoot>.Update.Set(name, parameter);
  return (int)collection.UpdateOneAsync(filter, set).Result.ModifiedCount;
  }
  catch (Exception e)
  {
  throw e;
  }
 }
 /// <summary>
 ///异步更新多个值
 /// </summary>
 /// <typeparam name="TAggregateRoot"></typeparam>
 /// <param name="filter"></param>
 /// <param name="Root"></param>
 /// <param name="property"></param>
 /// <param name="replace"></param>
 public int UpdateManyAsync<TAggregateRoot>(Expression<Func<TAggregateRoot, bool>> filter, TAggregateRoot Root, List<string> property = null, bool replace = false)
 {
  try
  {
  var collection = Db.GetCollection<TAggregateRoot>(typeof(TAggregateRoot).Name);
  var type = Root.GetType();
  //修改集合
  var list = new List<UpdateDefinition<TAggregateRoot>>();
  foreach (var propert in type.GetProperties())
  {
   if (propert.Name.ToLower() != "id")
   {
   try
   {
    if (property == null && property.Count < 1 || property.Any(o => o.ToLower() == propert.Name.ToLower()))
    {
    var replaceValue = propert.GetValue(Root);
    if (replaceValue != null)
     list.Add(Builders<TAggregateRoot>.Update.Set(propert.Name, replaceValue));
    else if (replace)
     list.Add(Builders<TAggregateRoot>.Update.Set(propert.Name, replaceValue));
    }
   }
   catch (Exception)
   {
    if (property == null)
    {
    var replaceValue = propert.GetValue(Root);
    if (replaceValue != null)
     list.Add(Builders<TAggregateRoot>.Update.Set(propert.Name, replaceValue));
    else if (replace)
     list.Add(Builders<TAggregateRoot>.Update.Set(propert.Name, replaceValue));
    }
   }

   }
  }
  if (list.Count > 0)
  {
   var builders = Builders<TAggregateRoot>.Update.Combine(list);
   return (int)collection.UpdateOneAsync(filter, builders).Result.ModifiedCount;
  }
  return 0;
  }
  catch (Exception e)
  {
  throw e;
  }
 }
 #endregion
 #endregion
 }
}

最后在我的仓库中取调用

 技术分享

技术分享

最后把mongodb的脚本文件贴上,欢迎大家给出好的建议,当然有更好的基于.net的mongoDB学习资源推荐给博主。

function HexToBase64(hex) {
 var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 var base64 = "";
 var group;
 for (var i = 0; i < 30; i += 6) {
 group = parseInt(hex.substr(i, 6), 16);
 base64 += base64Digits[(group >> 18) & 0x3f];
 base64 += base64Digits[(group >> 12) & 0x3f];
 base64 += base64Digits[(group >> 6) & 0x3f];
 base64 += base64Digits[group & 0x3f];
 }
 group = parseInt(hex.substr(30, 2), 16);
 base64 += base64Digits[(group >> 2) & 0x3f];
 base64 += base64Digits[(group << 4) & 0x3f];
 base64 += "==";
 return base64;
}

function Base64ToHex(base64) {
 var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 var hexDigits = "0123456789abcdef";
 var hex = "";
 for (var i = 0; i < 24;) {
 var e1 = base64Digits.indexOf(base64[i++]);
 var e2 = base64Digits.indexOf(base64[i++]);
 var e3 = base64Digits.indexOf(base64[i++]);
 var e4 = base64Digits.indexOf(base64[i++]);
 var c1 = (e1 << 2) | (e2 >> 4);
 var c2 = ((e2 & 15) << 4) | (e3 >> 2);
 var c3 = ((e3 & 3) << 6) | e4;
 hex += hexDigits[c1 >> 4];
 hex += hexDigits[c1 & 15];
 if (e3 != 64) {
  hex += hexDigits[c2 >> 4];
  hex += hexDigits[c2 & 15];
 }
 if (e4 != 64) {
  hex += hexDigits[c3 >> 4];
  hex += hexDigits[c3 & 15];
 }
 }
 return hex;
}

function UUID(uuid) {
 var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
 var base64 = HexToBase64(hex);
 return new BinData(4, base64); // new subtype 4
}

function JUUID(uuid) {
 var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
 var msb = hex.substr(0, 16);
 var lsb = hex.substr(16, 16);
 msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) + msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2);
 lsb = lsb.substr(14, 2) + lsb.substr(12, 2) + lsb.substr(10, 2) + lsb.substr(8, 2) + lsb.substr(6, 2) + lsb.substr(4, 2) + lsb.substr(2, 2) + lsb.substr(0, 2);
 hex = msb + lsb;
 var base64 = HexToBase64(hex);
 return new BinData(3, base64);
}

function CSUUID(uuid) {
 var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
 var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
 var b = hex.substr(10, 2) + hex.substr(8, 2);
 var c = hex.substr(14, 2) + hex.substr(12, 2);
 var d = hex.substr(16, 16);
 hex = a + b + c + d;
 var base64 = HexToBase64(hex);
 return new BinData(3, base64);
}

function PYUUID(uuid) {
 var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
 var base64 = HexToBase64(hex);
 return new BinData(3, base64);
}

BinData.prototype.toUUID = function () {
 var hex = Base64ToHex(this.base64()); // don‘t use BinData‘s hex function because it has bugs in older versions of the shell
 var uuid = hex.substr(0, 8) + ‘-‘ + hex.substr(8, 4) + ‘-‘ + hex.substr(12, 4) + ‘-‘ + hex.substr(16, 4) + ‘-‘ + hex.substr(20, 12);
 return ‘UUID("‘ + uuid + ‘")‘;
}

BinData.prototype.toJUUID = function () {
 var hex = Base64ToHex(this.base64()); // don‘t use BinData‘s hex function because it has bugs in older versions of the shell
 var msb = hex.substr(0, 16);
 var lsb = hex.substr(16, 16);
 msb = msb.substr(14, 2) + msb.substr(12, 2) + msb.substr(10, 2) + msb.substr(8, 2) + msb.substr(6, 2) + msb.substr(4, 2) + msb.substr(2, 2) + msb.substr(0, 2);
 lsb = lsb.substr(14, 2) + lsb.substr(12, 2) + lsb.substr(10, 2) + lsb.substr(8, 2) + lsb.substr(6, 2) + lsb.substr(4, 2) + lsb.substr(2, 2) + lsb.substr(0, 2);
 hex = msb + lsb;
 var uuid = hex.substr(0, 8) + ‘-‘ + hex.substr(8, 4) + ‘-‘ + hex.substr(12, 4) + ‘-‘ + hex.substr(16, 4) + ‘-‘ + hex.substr(20, 12);
 return ‘JUUID("‘ + uuid + ‘")‘;
}

BinData.prototype.toCSUUID = function () {
 var hex = Base64ToHex(this.base64()); // don‘t use BinData‘s hex function because it has bugs in older versions of the shell
 var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
 var b = hex.substr(10, 2) + hex.substr(8, 2);
 var c = hex.substr(14, 2) + hex.substr(12, 2);
 var d = hex.substr(16, 16);
 hex = a + b + c + d;
 var uuid = hex.substr(0, 8) + ‘-‘ + hex.substr(8, 4) + ‘-‘ + hex.substr(12, 4) + ‘-‘ + hex.substr(16, 4) + ‘-‘ + hex.substr(20, 12);
 return ‘CSUUID("‘ + uuid + ‘")‘;
}

BinData.prototype.toPYUUID = function () {
 var hex = Base64ToHex(this.base64()); // don‘t use BinData‘s hex function because it has bugs
 var uuid = hex.substr(0, 8) + ‘-‘ + hex.substr(8, 4) + ‘-‘ + hex.substr(12, 4) + ‘-‘ + hex.substr(16, 4) + ‘-‘ + hex.substr(20, 12);
 return ‘PYUUID("‘ + uuid + ‘")‘;
}


BinData.prototype.toHexUUID = function () {
 var hex = Base64ToHex(this.base64()); // don‘t use BinData‘s hex function because it has bugs
 var uuid = hex.substr(0, 8) + ‘-‘ + hex.substr(8, 4) + ‘-‘ + hex.substr(12, 4) + ‘-‘ + hex.substr(16, 4) + ‘-‘ + hex.substr(20, 12);
 return ‘HexData(‘ + this.subtype() + ‘, "‘ + uuid + ‘")‘;
}

 

在.net下打造mongoDb基于官方驱动最新版本

标签:names   bool   private   技术   .net   top   namespace   efault   sea   

热心网友 时间:2022-05-02 02:44

可以支持

有很多的mongdb 的驱动程序

网页链接   这是nuget 的链接,引用一个就可以用了

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
畅享好还是荣耀好 怎么向老师请教问题?? ...和是40.15较大数的小数点向左移动一位就等于较小的数,这两_百度知... 两个数的和是40.15,较大数的小数点向左移动一位就等于较小的数这两... 两个数的和是40.15,较大的数的小数点像向左移动一位就等于较小的数... 两个数的和是40.15,较大数的小数点向左移动一位等于较小的数 ...和是40.15较大数的小数点向左移动一位就等于较小的数这两个数分别... 怎么用手机查询农行信用卡余额? 朝阳公园附近有孩子可以托管的地方吗? 华泰证券怎么添加第二张银行卡 有什么简单变白的方法 mQ8E2ch/A是手机什么型号 苹果6序列号查真伪F17Q6E93G5MQ是正品吗 余利宝额度100万额度怎么获得 谁知道哪里有《献给爱丽丝》的简谱。或给我发E-mail:mqh365@sina.com 苹果6 F19PT9E3G5MQ 生产时间等? 怎么样变白?要普通的那种方法·效果最好好一点 余利宝转入额度100万是每年的吗 帮我查苹果6的激活日期,序列号是f78p9eh7g5mq 余利宝累计发放额度是什么 急急急!!!关于使用loadrunner测试MQ的问题 E=Q=mq E什么意思 笔记本电脑Intel处理器酷睿i系列产品型号中数字后的XM,qm,k,m,e,u,y,HQ,MQ, 中国网通跟中国联通是什么关系 海南乐秀-MQ-E是什么 app store应用视频打不开 用APP看视频,内部网查得到吗? 华为平板点击视频时为什么突然让用其他APP软件打开视频? 苹果手机里的mp4视频可以用其他app打开吗 ipad本地视频怎么用别的app打开 苏mq750e有没有贷款 余利宝4000一天多少 http:&#47;&#47;www.tudou.com&#47;programs&#47;view&#47;E4mhMQg2EyY&#47;麦蒂视频里的是什么歌?本人跪求 环境工程 污水处理厂工艺设计毕业设计 希望高手帮帮忙!谢谢! 环境工程专业 本科毕业搞论文,课题是某市几个污水处理厂处理工艺的比较(调查类的),从哪几方面下手比 成人教育环境工程专业毕业论文 谁能给我几本关于污水处理的书给我作为毕业论文的参考文献! 环境工程专业的学长学姐帮忙!!! 求污水处理毕业设计一份,图纸和论文都可以 剧中果郡王风度翩翩被雍正忌恨,真实的果郡王是怎样的? 崔真实为什么自杀 历史上真实的果郡王是什么样 美的净水器mro203-4出的水可以直饮吗 美的净水器MRO203-4好用吗 c# MongoDB 驱动2.4.4版本中没有eval方法,如何在代码中执行mongo脚本? 美的mro203-4净水器老是断电 美的MRO203-4净水器怎么样,滤芯咋那么贵。 美的反渗透净水器MRO203-4怎么样 美的净水器mro203-4好不好 美的mro203-4净水机换电源后不制水而且通电后进水阀还是关闭。用电笔测阀门2个接头都亮灯。 纯水机报价和品牌型号推荐