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 = ;
data.name = "江北";
data.age = ;
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 = ;
student1.name = "北晚舟";
student1.age = ;
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, );//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 > );
FindOptions<Student, Student> findOpt = new FindOptions<Student, Student>();
findOpt.Limit = ;
findOpt.Skip = ;
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 > );
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 > );
//delete from Student where age>21
//student.DeleteMany(filter);
student.DeleteOne(filter);//只删除一个

MongoDB中文网:https://www.mongodb.org.cn

 

.Net Core中简单使用MongoDB的更多相关文章

  1. C# Asp.net中简单操作MongoDB数据库(一)

    需要引用MongoDB.Driver.dll.MongoDB.Driver.core.dll.MongoDB.Bson.dll三个dll. 1.数据库连接: public class MongoDb ...

  2. .NET平台开源项目速览(20)Newlife.Core中简单灵活的配置文件

    记得5年前开始拼命翻读X组件的源码,特别是XCode,但对Newlife.Core 的东西了解很少,最多只是会用用,而且用到的只是九牛一毛.里面好用的东西太多了. 最近一年时间,零零散散又学了很多,也 ...

  3. C# Asp.net中简单操作MongoDB数据库(二)

    C# Asp.net中简单操作MongoDB数据库(一)    , mongodb数据库连接可以回顾上面的篇幅. 1.model类: public class BaseEntity { /// < ...

  4. .net core 中简单封装Dapper.Extensions 并使用sqlsuger自动生成实体类

    引言 由公司需要使用dapper  同时支持多数据库 又需要支持实体类 又需要支持sql 还需要支持事务 所以采用了 dapper + dapperExtensions  并配套 生成实体类小工具的方 ...

  5. Asp.Net Core中简单使用日志组件log4net

    本文将简单介绍在.NET 6中使用log4net的方法,具体见下文范例. 1.首先新建一个ASP.NET Core空项目 2.通过Nuget包管理器安装下面两个包 log4net Microsoft. ...

  6. 在.Net Core中使用MongoDB的入门教程(一)

    首先,我们在MongoDB的官方文档中看到,MongoDb的2.4以上的For .Net的驱动是支持.Net Core 2.0的. 所以,在我们安装好了MangoDB后,就可以开始MangoDB的.N ...

  7. Asp.Net Core中使用MongoDB的入门教程,控制台程序使用 MongoDB

    内容来源  https://blog.csdn.net/only_yu_yy/article/details/78882446 首先,创建一个.Net Core的控制台应用程序.然后使用NuGet导入 ...

  8. 在Asp.Net Core中添加区域的简单实现

    使用区域,可以有效的对业务进行隔离,各种业务及分工可以更灵活.在Asp.Net Core中启用区域也是极简单的. 使用步骤: 1.在 Startup.cs 中添加区域的路由: app.UseMvc(r ...

  9. Hangfire在ASP.NET CORE中的简单实现

    hangfire是执行后台任务的利器,具体请看官网介绍:https://www.hangfire.io/ 新建一个asp.net core mvc 项目 引入nuget包 Hangfire.AspNe ...

随机推荐

  1. Git篇---将秘钥添加到github

    第1步:创建SSH Key.在用户主目录下,看看有没有-/.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步.如果没有,打开She ...

  2. 论TEMP临时变量与VAR静态变量区别

    TEMP临时变量:顾名思义,这种变量类型是临时的,没有固定的存放数据的内存空间.每次扫描结束后则清零,在下个扫描周期开始时,这个变量的值都是不确定的,一般为0.使用临时变量需要遵循一个原则:先赋值再使 ...

  3. java 面向对象(三十三):泛型二 泛型在集合中的使用

    1. 在集合中使用泛型之前的例子 @Test public void test1(){ ArrayList list = new ArrayList(); //需求:存放学生的成绩 list.add( ...

  4. 机器学习实战基础(十八):sklearn中的数据预处理和特征工程(十一)特征选择 之 Wrapper包装法

    Wrapper包装法 包装法也是一个特征选择和算法训练同时进行的方法,与嵌入法十分相似,它也是依赖于算法自身的选择,比如coef_属性或feature_importances_属性来完成特征选择.但不 ...

  5. tensorflow实现lstm中遇到的函数记录

    函数一:initializer=tf.random_uniform_initializer(-0.1, 0.1, seed=123) tf.random_uniform_initializer 参数: ...

  6. Python 图像处理 OpenCV (14):图像金字塔

    前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...

  7. 京东秋招提前批初试--java开发工程师

    1,自我介绍,学过的课程有哪些 2,介绍一下java的内存结构和内存模型(jvm和jmm) 3,对于NIO有没有了解?作用是什么?(基于通道和缓冲区的I/O方式,用的是DirectByteBuffer ...

  8. Java8——Stream流

    Stream是数据渠道,用于操作集合.数组等生成的元素序列. Stream操作的三个步骤: 创建Stream 中间操作 终止操作 一.获取stream的四种方式 通过collection系列集合的st ...

  9. Python Ethical Hacking - Malware Packaging(1)

    PACKAGING Convert python program into an executable that: Packages all program files into a single e ...

  10. Go的100天之旅-08字符串

    目录 简介 UTF-8字符 字符串的常用操作 简介 字符串在各种编程语言中都是很基础的一种类型,在Go中字符串简单理解就是一个数组,数组里面的元素是byte类型.因此基本上拥有类似数组的全部特性.例如 ...