MongoDb C# Wrapper 类 (MongoDb Driver 1.9)
程序员文章站
2022-04-25 11:06:05
...
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<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)的内容,更多相关内容请关注PHP中文网(www.php.cn)!
上一篇: 如何能在php页面上显示服务器上文件名字
下一篇: php5类中三种数据类型的区别
推荐阅读
-
MongoDB .Net Driver(C#驱动) - 内嵌数组/嵌入文档的操作(增加、删除、修改、查询(Linq 分页))
-
MongoDB 3.0 关于安全认证后使用C#调用碰上“System.TimeoutException”类型的异常在 MongoDB.Driver.Core.d
-
基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类)
-
C#操作MongoDB的帮助类-简单封装
-
MongoDB C# Driver 使用示例 (2.2)
-
扩展MongoDB C# Driver的QueryBuilder
-
MongoDB .Net Driver(C#驱动) - 内嵌数组/嵌入文档的操作(增加、删除、修改、查询(Linq 分页))
-
MongoDB 3.0 关于安全认证后使用C#调用碰上“System.TimeoutException”类型的异常在 MongoDB.Driver.Core.d
-
MongoDB Java Driver 源码分析(11):GridFS 类
-
MongoDb C# Wrapper 类 (MongoDb Driver 1.9)