.Net Core如何使用MongoDB
小编这次要给大家分享的是.Net Core如何使用MongoDB,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。
前言
MongoDB 是由C++语言编写的,是一个基于分布式且面向文档存储的开源数据库系统。
在.Net Core中使用需要引入核心包 MongoDB.Driver
添加数据:
//与Mongodb建立连接MongoClient client = new MongoClient("mongodb://127.0.0.1");//获得数据库,没有则自动创建IMongoDatabase db = client.GetDatabase("db1");//拿到集合(表)IMongoCollection<Student> student = db.GetCollection<Student>("Student");var data = new Student();data.id = 1;data.name = "江北";data.age = 22;data.remarks = "暂无";//添加一条数据student.InsertOne(data);
在图形化界面中查看一下
Mongodb默认用id做主键,因此不会显式的指定id是主键。Mongdb中没有内置"自增字段",可以把id声明为ObjectId类型,这样插入以后就自动给字段赋值。
例如,建一个类:
public class School{ public ObjectId id { get; set; } public string name { get; set; } public string address { get; set; }}//需引入命名空间using MongoDB.Bson;
当然School对象之后多加或者去掉一个字段都行。Mongodb是用Json保存的,因此也可以直接用Json格式插入,可用BsonDocument对象作为泛型对象。
//与Mongodb建立连接MongoClient client = new MongoClient("mongodb://127.0.0.1");//获得数据库,没有则自动创建IMongoDatabase db = client.GetDatabase("db1");//拿到集合(表)IMongoCollection<BsonDocument> document = db.GetCollection<BsonDocument>("School");db.GetCollection<BsonDocument>("School");var json = "{id:1,name:'xx学校',address:'xxx路xx号',remarks:'暂无!'}";BsonDocument bsons = BsonDocument.Parse(json);
学生和学校是有对应关系的,我们可以添加有嵌套关系类型的对象
public class Student{ public int id { get; set; } public string name { get; set; } public int age { get; set; } public string remarks { get; set; } public School School { get; set; }}
//与Mongodb建立连接MongoClient client = new MongoClient("mongodb://127.0.0.1");//获得数据库,没有则自动创建IMongoDatabase db = client.GetDatabase("db1");//拿到集合(表)IMongoCollection<Student> student = db.GetCollection<Student>("Student");Student student1 = new Student();student1.id = 2;student1.name = "北晚舟";student1.age = 22;student1.remarks = "暂无";School school = new School();school.name = "xxxSchool";school.address = "xxxAddress";student1.School = school;student.InsertOne(student1);
数据查询:
//与Mongodb建立连接MongoClient client = new MongoClient("mongodb://127.0.0.1");//获得数据库,没有则自动创建IMongoDatabase db = client.GetDatabase("db1");//拿到集合(表)IMongoCollection<Student> student = db.GetCollection<Student>("Student");var data = Builders<Student>.Filter.Gt(m => m.age, 21);//Gt:大于var result = student.Find(data).ToList();
我们安装的NuGet包是支持Lamda表达式的,可用条件表达式来查找数据
//与Mongodb建立连接MongoClient client = new MongoClient("mongodb://127.0.0.1");//获得数据库,没有则自动创建IMongoDatabase db = client.GetDatabase("db1");//拿到集合(表)IMongoCollection<Student> student = db.GetCollection<Student>("Student");var data = Builders<Student>.Filter.Where(m => m.age > 21 && m.name.Contains("江"));var result = student.Find(data).ToList();
分页查询:
//与Mongodb建立连接MongoClient client = new MongoClient("mongodb://127.0.0.1");//获得数据库,没有则自动创建IMongoDatabase db = client.GetDatabase("db1");//拿到集合(表)IMongoCollection<Student> student = db.GetCollection<Student>("Student");var filter = Builders<Student>.Filter.Where(m => m.age > 21);FindOptions<Student, Student> findOpt = new FindOptions<Student, Student>();findOpt.Limit = 2;findOpt.Skip = 1;findOpt.Sort = Builders<Student>.Sort.Ascending(m => m.age).Descending(m => m.name);var result = (student.FindAsync(filter, findOpt).Result).ToList();
数据更新:
//与Mongodb建立连接MongoClient client = new MongoClient("mongodb://127.0.0.1");//获得数据库,没有则自动创建IMongoDatabase db = client.GetDatabase("db1");//拿到集合(表)IMongoCollection<Student> student = db.GetCollection<Student>("Student");var filter = Builders<Student>.Filter.Where(m => m.age > 21);var update = Builders<Student>.Update.Set(m => m.name, "皮卡丘");//update Student set name="皮卡丘" where age>21student.UpdateMany(filter, update);
数据删除:
//与Mongodb建立连接MongoClient client = new MongoClient("mongodb://127.0.0.1");//获得数据库,没有则自动创建IMongoDatabase db = client.GetDatabase("db1");//拿到集合(表)IMongoCollection<Student> student = db.GetCollection<Student>("Student");var filter = Builders<Student>.Filter.Where(m => m.age > 21);//delete from Student where age>21//student.DeleteMany(filter);student.DeleteOne(filter);//只删除一个
看完这篇关于.Net Core如何使用MongoDB的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。