Mongodb是一种比较常见的NOSQL数据库,数据库排名第四,今天介绍一下Net Core 下,常规操作。
首先下C# 版的驱动程序 “MongoDB.Driver”,相关依赖包自行引用进来。 过于细节的基础知识就不讲了,直接就奔主题了
using MongoDB.Driver; //命名空间 MongoClient client = new MongoClient("mongodb://192.168.99.5"); IMongoDatabase dbBase = client.GetDatabase("School"); Mongodb有一个特性,你可以不用先建数据库和集合,数据库会自行检查,如果没有就新建。
IMongoCollection<> collection = dbBase.GetCollection<>("Class"); //获取集合 GetCollection是一个泛型方法,你可以输入BsonDocument,或者实体,Mongodb驱动程序会自动帮你序列化 IMongoCollection<BsonDocument> collection = dbBase.GetCollection<BsonDocument>("Class"); 复制代码 public class Class { public BsonObjectId Id { set; get; }
public string ClassName { set; get; }
public List<Student> StudentItems { set; get; } }
public class Student { public string Name { set; get; }
public int Age { set; get; }
public string Sex { set; get; } }
IMongoCollection<Class> collection = dbBase.GetCollection<Class>("Class"); 复制代码 如果你使用BsonDocument,那么你会得么一个BsonDocument对象,Mongodb驱动程序内置了序列化方法,方便将BsonDocument转换成实体对象。
using MongoDB.Bson.Serialization;//命名空间 var schoolClass = BsonSerializer.Deserialize<Class>(new BsonDocument()); Mongodb驱动程序支持Linq 语法,你可以借此写出优雅语句。
var schoolClass = collection.Find(n => n.ClassName == "一年级").FirstOrDefault(); var schoolClass2 = collection.AsQueryable<Class>().Where(n => n.ClassName == "一年级").FirstOrDefault(); var schoolClass3 = collection.Find(new BsonDocument("ClassName", "一年级")).FirstOrDefault(); Mongodb驱动程序还支持异步方法,根据命名约定,很多方法相应的Async结尾的,都是异步方法。
var schoolClass = await collection.Find(n => n.ClassName == "一年级").FirstOrDefaultAsync(); var schoolClass2 = await collection.AsQueryable<Class>().Where(n => n.ClassName == "一年级").FirstOrDefaultAsync(); var schoolClass3 = await collection.Find(new BsonDocument("ClassName", "一年级")).FirstOrDefaultAsync(); 基本介绍完毕,下面进入增删更改的操作方法。
---------------------------增加---------------------------------
复制代码 IMongoCollection<Class> collection = dbBase.GetCollection<Class>("Class"); //写入单个 collection.InsertOne(new Class { ClassName = "二年级", StudentItems = new List<Student> { new Student{Age=10,Name="李明",Sex="男" }, new Student{Age=12,Name="小红",Sex="女" } } }); //批量写入 collection.InsertMany(new List<Class> { new Class { ClassName = "二年级", StudentItems = new List<Student> { new Student{Age=10,Name="李明",Sex="男" }, new Student{Age=12,Name="小红",Sex="女" } } } }); 复制代码
复制代码 IMongoCollection<BsonDocument> collection = dbBase.GetCollection<BsonDocument>("Class"); //写入单个 collection.InsertOne(new BsonDocument { {"ClassName","二年级" }, {"StudentItems",new BsonArray{ new BsonDocument { { "Age", 10 },{ "Name", "李明" },{ "Sex", "男" } } } } }); //批量写入 collection.InsertMany(new List<BsonDocument> { new BsonDocument { {"ClassName","二年级" }, {"StudentItems",new BsonArray{ new BsonDocument { { "Age", 10 },{ "Name", "李明" },{ "Sex", "男" } } } } }, new BsonDocument { {"ClassName","二年级" }, {"StudentItems",new BsonArray{ new BsonDocument { { "Age", 10 },{ "Name", "李明" },{ "Sex", "男" } } } } } }); 复制代码
不过,使用C#语言,我还是尽量推荐使用实体来添加,可读性,维护性也强。也有缺点,JSON结构如果使用实体话,类库较大,庆幸的Mongodb驱动程序可以帮你序列化。
BsonDocument结构虽然符合JSON,用起来却很痛苦,不过也减少类库的数量。
---------------------------删除---------------------------------
复制代码 IMongoCollection<Class> collection = dbBase.GetCollection<Class>("Class"); //删除单个 collection.DeleteOne(n => n.ClassName == "二年级");//使用拉姆达表达式 collection.DeleteOne(new BsonDocument("ClassName", "二年级"));//使用BsonDocument var filter = Builders<Class>.Filter.Eq("_id", new BsonObjectId("5a545e9381a98c3ab46c9cb9")); collection.DeleteOne(filter);//使用Builders, //批量删除 collection.DeleteMany(n => n.ClassName == "二年级"); collection.DeleteMany(new BsonDocument("ClassName", "二年级")); collection.DeleteMany(filter); //删除并返回 collection.FindOneAndDelete(n => n.ClassName == "三年级"); 复制代码
复制代码 IMongoCollection<BsonDocument> collection = dbBase.GetCollection<BsonDocument>("Class"); //删除单个 collection.DeleteOne(new BsonDocument("ClassName", "二年级"));//使用BsonDocument var filter = Builders<BsonDocument>.Filter.Eq("_id", new BsonObjectId("5a545e9381a98c3ab46c9cb9")); collection.DeleteOne(filter);//使用Builders, //批量删除 collection.DeleteMany(new BsonDocument("ClassName", "二年级")); collection.DeleteMany(filter); //删除并返回 collection.FindOneAndDelete(new BsonDocument("ClassName", "二年级")); 复制代码
使用BsonDocument,就不能使用酷酷的拉姆达表达式了,其它的差不多。
---------------------------更改---------------------------------
复制代码 IMongoCollection<Class> collection = dbBase.GetCollection<Class>("Class"); //修改单个 var filter = Builders<Class>.Filter.Eq("_id", new BsonObjectId("5a545e9381a98c3ab46c9cb8")); var update = Builders<Class>.Update.Set("ClassName", "三年级"); var update2 = Builders<Class>.Update.Set(n => n.ClassName, "三年级");
collection.UpdateOne(n => n.ClassName == "二年级", update);//使用拉姆达表达式 collection.UpdateOne(filter, update2);//使用Builders //批量修改 collection.UpdateMany(n => n.ClassName == "二年级", update); collection.UpdateMany(filter, update2); //替换 var schoolClass = collection.Find(n => n.ClassName == "二年级").FirstOrDefault(); collection.ReplaceOne(n => n.ClassName == "二年级", schoolClass); 复制代码
使用BsonDocument 也是相应差不多,我就不贴出代码了。
修改数组,则大概如下
复制代码 //数组增加一个 var update = Builders<Class>.Update.Push("StudentItems", new Student { Age = 18, Name = "张明", Sex = "男" }); collection.UpdateOne(n => n.ClassName == "三年级", update); //数组减少一个 var update2 = Builders<Class>.Update.Pull(n => n.StudentItems, new Student { Age = 18, Name = "张明", Sex = "男" }); collection.UpdateOne(n => n.ClassName == "三年级", update2); //数组修改一个 var update3 = Builders<Class>.Update.Set("StudentItems.$.Age", 30); collection.UpdateOne(n => n.ClassName == "三年级" && n.StudentItems.Any(m=>m.Age==10), update3); var update4 = Builders<Class>.Update.Set("StudentItems.$", new Student { Age = 18, Name = "张明", Sex = "男" }); collection.UpdateOne(n => n.ClassName == "三年级" && n.StudentItems.Any(m => m.Age == 10), update4); 复制代码
示例是筛选过,不能每种写法写出来,读者自行试尝组合
---------------------------查询---------------------------------
复制代码 var shoolClass = collection.Find(n => n.ClassName == "三年级").FirstOrDefault();//使用拉姆达表达式 var shoolClassItems = collection.Find(n => n.ClassName == "三年级").ToList(); var shoolClass2 = collection.Find(new BsonDocument("ClassName", "三年级")).FirstOrDefault(); var shoolClassItems2 = collection.Find(new BsonDocument("ClassName", "三年级")).ToList(); var filter = Builders<Class>.Filter.Eq(n => n.ClassName, "三年级");//使用Builders var shoolClass3 = collection.Find(filter).FirstOrDefault(); var shoolClassItems3 = collection.Find(filter).ToList(); 复制代码
Mongodb的基本应用,就讲解完毕,笔记一下
|