MongoDb C# Wrapper 類 (MongoDb Driver 1.9)

來源:互聯網
上載者:User
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)的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.