前言
MongoDB 是由C++语言编写的,是一个基于分布式且面向文档存储的开源数据库系统。
下载地址:
https://www.mongodb.com/download-center/community
在.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>21 student.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);//只删除一个
MongoDB中文网:https://www.mongodb.org.cn
总结
到此这篇关于.Net Core使用MongoDB的完整步骤的文章就介绍到这了,更多相关.Net Core使用MongoDB内容请搜索阿兔在线工具以前的文章或继续浏览下面的相关文章希望大家以后多多支持阿兔在线工具!