前言

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内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!