• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

MongoDb C# Wrapper 类 (MongoDb Driver 1.9)

c# 搞代码 4年前 (2022-01-09) 10次浏览 已收录 0个评论

1.安装 mongoDb Driver package

2. 使用Wrapper 类:

 public class MongoDbWrapper : IDisposable    {        private MongoServer _server;        private MongoDatabase _db;        public MongoDbWrapper()        {            var uri = ConfigurationSettings.AppSettings["mongoUrl"];            var url = new MongoUrl(uri);            var client = new MongoClient(url);            _server = client.GetServer();            _db = _server.GetDatabase(url.DatabaseName);        }        public MongoDbWrapper BatchAdd<T>(T[] objArray, string collectionName)        {            var collection = _db.GetCollection&<p style="color:transparent">本文来源gao!%daima.com搞$代*!码$网3</p>lt;T>(collectionName);            collection.InsertBatch(objArray);            return this;        }        public MongoDbWrapper Add<T>(T obj, string collectionName)        {            var collection = _db.GetCollection<T>(collectionName);            collection.Insert(obj);            return this;        }        /// <summary>        /// e.g. { "Age", new BsonDocument { { "$gte", 10 } } }        /// </summary>        /// <param name="query"></param>        /// <param name="collectionName"></param>        public void DeleteBy<T>(Expression<Func<T, bool>> whereExp, string collectionName)        {            var collection = _db.GetCollection<T>(collectionName);            collection.Remove(Query<T>.Where(whereExp));        }        public void Update<T>(IMongoQuery query, string collectionName, T newObj) where T : IMongoUpdate        {            var collection = _db.GetCollection<T>(collectionName);            collection.Update(query, newObj);        }        public IEnumerable<T> Search<T>(Expression<Func<T, bool>> whereExp, string collectionName)        {            var collection = _db.GetCollection<T>(collectionName);            return collection.Find(Query<T>.Where(whereExp)).ToList();        }        public T Single<T>(Expression<Func<T, bool>> whereExp, string collectionName)        {            return Search(whereExp, collectionName).Single();        }        public void RemoveCollection(string collectionName)        {            _db.DropCollection(collectionName);        }        public void Dispose()        {            _server.Disconnect();        }    }

3 一些相关操作的用法示例

查询var driver = dbWrapper.Single<Driver>(d => d.Name == name, DbCollectionName.For<Driver>());var drivers = dbWrapper.Search<Driver>(d => d.Name == name, DbCollectionName.For<Driver>());删除dbWrapper.DeleteBy<Job>(j => j.Id == id, DbCollectionName.For<PublicQueue>());从集合移除var updatingNw = Update<Network>.Pull(nw => nw.Jobs, aJobFromQueue);                dbWrapper.Update(Query<Network>.Where(n => n.Name == Name), DbCollectionName.For<Network>(), updatingNw);添加新项到集合var updatingDp = Update<Dispatcher>.AddToSet<dynamic>(d => d.PendingJobs, aJobFromQueue);                dbWrapper.Update(Query<Dispatcher>.Where(d => d.Name == name), DbCollectionName.For<Dispatcher>(), updatingDp);更新dbWrapper.Update(Query<Network>.Where(n => n.Name == Name), DbCollectionName.For<Network>(), updatingNw);

以上就是MongoDb C# Wrapper 类 (MongoDb Driver 1.9)的内容,更多相关内容请关注搞代码(www.gaodaima.com)!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MongoDb C# Wrapper 类 (MongoDb Driver 1.9)
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址