一.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); } }