C# 操作 Mongodb
2021/4/9 2:27:09
本文主要是介绍C# 操作 Mongodb,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一.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); } }
这篇关于C# 操作 Mongodb的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具