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. 诊断Java线程死锁

    比如我们有运行这样一个程序: 了解多线程的小伙版都知道,这段代码不会有打印结果,因为发生了死锁.我们在服务器上运行试试,没有输出,对应的进程是 32752. 使用 “jstack 32752”排查,后 ...

  2. python中pymysql executemany 批量插入数据

    import pymysqlimport timedb = pymysql.connect("IP","username","password&quo ...

  3. CMDB04 /流程梳理、cmdb总结

    CMDB04 /流程梳理.cmdb总结 目录 CMDB04 /流程梳理.cmdb总结 1. 流程梳理 1.1 环境 1.2 远程连接服务器 1.3 向服务器上传文件 1.4 运维管理服务器 2. cm ...

  4. Python之介绍、基本语法、流程控制

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  5. 数据可视化实例(十二): 发散型条形图 (matplotlib,pandas)

    https://datawhalechina.github.io/pms50/#/chapter10/chapter10 如果您想根据单个指标查看项目的变化情况,并可视化此差异的顺序和数量,那么散型条 ...

  6. HotSpot的对象模型(6)

    接着上一篇,我们继续来讲oopDesc相关的子类. 3.instanceOopDesc类 instanceOopDesc类的实例表示除数组对象外的其它对象.在HotSpot中,对象在内存中存储的布局可 ...

  7. js获取url并截取相应的字段,js解决url获取中文字段乱码问题

    相信url截取信息是一个很常用的小功能页面跳转传参的时候可以在A页面的url挂一些参数到B页面获取正常的页面传参都是以数字和英文为主正常情况下中文获取的时候是有乱码的所谓上有政策下有对策一个正常的ur ...

  8. Cmd重定向

    1.执行单条cmd命令 public static string ExecuteCmd(string command) { Process p = new Process(); p.StartInfo ...

  9. ADB-常见命令使用详解

    ADB命令使用详解 ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 1.连接android设置adb connect 设备名例如:adb con ...

  10. easyUI传递参数

    #======================JSP=====================================                <div class="l ...