C# 操作 Mongodb

互联网 2021/4/9 2:27:09

一.nuget引用MongoDB.Driver 二.创建连接//mongodb://用户名:密码@ip:端口 var client = new MongoClient("mongodb://mogo:mogo123@127.0.0.1:27017"); var database = client.GetDatabase("testDB"); var collection = database.GetCollection<…

一.nuget引用MongoDB.Driver

二.创建连接

   //mongodb://用户名:密码@ip:端口 
var client = new MongoClient("mongodb://mogo:mogo123@127.0.0.1:27017");
var database = client.GetDatabase("testDB"); 
var collection = database.GetCollection<BsonDocument>("testCol");

三.增删改查

1.增加

var document = new BsonDocument
            {
                { "age", 23},
                { "name", "fff" },
                { "gender", "女" }
            };
collection.InsertOne(document);

 

2.删除

collection.DeleteMany(Builders<BsonDocument>.Filter.Eq("age", 23))

 

3.修改

collection.UpdateMany(Builders<BsonDocument>.Filter.Eq("age", 23), Builders<BsonDocument>.Update.Set("name", "www"));

 

4.查

collection.Find(Builders<BsonDocument>.Filter.Eq("name", "fff") & Builders<BsonDocument>.Filter.Lt("age", 20));

这是我封装的一个MongoHelper

    public class MongoHelper
    {
        private static string MongoConnection = ConfigurationManager.AppSettings["MongoConnection"].ToString();
        private IMongoDatabase _database { get; set; }
        public MongoHelper()
        {
            //mongodb://用户名:密码@ip:端口
            //mongodb://mogo:mogo123@127.0.0.1:27017
            var client = new MongoClient(MongoConnection);
            _database = client.GetDatabase("testDb");

         
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">数据集合</param>
        public void InsertMany<T>(List<T> list)
        {
            var collection = _database.GetCollection<T>(typeof(T).Name);
            collection.InsertMany(list);
        }

        /// <summary>
        /// 单条插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">数据集合</param>
        public void InsertOne<T>(T obj)
        {
            var collection = _database.GetCollection<T>(typeof(T).Name);
            collection.InsertOne(obj);
        }
        /// <summary>
        /// 获取一个文档
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filter">查询条件</param>
        /// <param name="projection">投影(字段筛选)</param>
        /// <returns></returns>
        public T GetFirstOrDefault<T>(FilterDefinition<T> filter, ProjectionDefinition<T> projection)
        {
            return _database.GetCollection<T>(typeof(T).Name).Find(filter).Project<T>(projection).FirstOrDefault();
        }

        /// <summary>
        /// 获取全部文档
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filter">查询条件</param>
        /// <param name="projection">投影(字段筛选)</param>
        /// <returns></returns>
        public List<T> GetToList<T>(FilterDefinition<T> filter, ProjectionDefinition<T> projection)
        {
            return _database.GetCollection<T>(typeof(T).Name).Find(filter).Project<T>(projection).ToList();
        }
        /// <summary>
        /// 替换整个文档
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">替换得文档</param>
        /// <param name="filter">查询条件</param>
        public void ReplaceOne<T>(FilterDefinition<T> filter, T item)
        {
            _database.GetCollection<T>(typeof(T).Name).ReplaceOne(filter, item);
        }
        /// <summary>
        /// 批量替换整个文档
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">替换得文档</param>
        /// <param name="filter">查询条件</param>
        public void ReplaceMany<T>(FilterDefinition<T> filter, T item)
        {
            //查询是否存在,并得到查询结果
            var cont = _database.GetCollection<T>(typeof(T).Name).Find(filter).CountDocuments();
            if (cont != 0)
            {
                for (int i = 0; i < cont; i++)
                {
                    // BsonDocument updatedocument = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(item.ToJson());
                    // BsonDocument updatedocument = item.ToBsonDocument();
                    _database.GetCollection<T>(typeof(T).Name).ReplaceOne(filter, item);
                }
            }
        }
        /// <summary>
        /// 更新单条
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        public void UpdateOne<T>(FilterDefinition<T> filter, UpdateDefinition<T> obj)
        {
            var collection = _database.GetCollection<T>(typeof(T).Name);
            collection.UpdateOne(filter, obj);

        }
        /// <summary>
        /// 更新多条
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        public void UpdateMany<T>(FilterDefinition<T> filter, UpdateDefinition<T> obj)
        {
            var collection = _database.GetCollection<T>(typeof(T).Name);
            collection.UpdateMany(filter, obj);
        }

        /// <summary>
        /// 删除一条
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        public void DeleteOne<T>(FilterDefinition<T> filter)
        {
            var collection = _database.GetCollection<T>(typeof(T).Name);
            collection.DeleteOne(filter);
        }

        /// <summary>
        /// 删除多条
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        public void DeleteMany<T>(FilterDefinition<T> filter)
        {
            var collection = _database.GetCollection<T>(typeof(T).Name);
            collection.DeleteMany(filter);
        }
    }

 

随时随地学软件编程-关注百度小程序和微信小程序
关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[C# 操作 Mongodb]http://www.zyiz.net/tech/detail-154111.html

上一篇:C#编程——基于TCP的套接字通信包【开源】

下一篇:学一手,知乎大V(轮子哥)当年靠它进微软亚洲研究院

赞(0)

共有 条评论 网友评论

验证码: 看不清楚?
    关注微信小程序
    程序员编程王-随时随地学编程

    扫描二维码或查找【程序员编程王】

    可以随时随地学编程啦!

    技术文章导航 更多>
    扫一扫关注最新编程教程