MongoDb C # Wrapper class,

Source: Internet
Author: User
Tags mongodb driver

MongoDb C # Wrapper class,

1. Install mongoDb Driver package

2. Use the Wrapper class:


 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 examples of related operations

Query var driver = dbWrapper. single <Driver> (d => d. name = name, DbCollectionName. for <Driver> (); var drivers = dbWrapper. search <Driver> (d => d. name = name, DbCollectionName. for <Driver> (); Delete dbWrapper. deleteBy <Job> (j => j. id = id, DbCollectionName. for <PublicQueue> (); remove var updatingNw = Update from the set <Network>. pull (nw => nw. jobs, aJobFromQueue); dbWrapper. update (Query <Network>. where (n => n. name = Name), DbCollectionName. for <Network> (), updatingNw); add a new item to the set var updatingDp = Update <Dispatcher>. addToSet <dynamic> (d => d. pendingJobs, aJobFromQueue); dbWrapper. update (Query <Dispatcher>. where (d => d. name = name), DbCollectionName. for <Dispatcher> (), updatingDp); Update dbWrapper. update (Query <Network>. where (n => n. name = Name), DbCollectionName. for <Network> (), updatingNw );


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.