zoukankan      html  css  js  c++  java
  • .Net Core中使用MongoDB

    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

     

  • 相关阅读:
    IE6/IE7浏览器中"float: right"自动换行的解决方法
    IE6/IE7浏览器不支持display: inline-block;的解决方法
    如何解决两个li之间的缝隙
    input、button、a标签 等定义的按钮尺寸的兼容性问题
    在一个页面重复使用一个js函数的方法
    关于让input=text,checkbox居中的解决方法
    遮盖层实现(jQuery+css+html)
    button,input type=button按钮在IE和w3c,firefox浏览器区别
    前端-选项卡(菜单栏)
    形成人、机器、过程和数据的互联互通
  • 原文地址:https://www.cnblogs.com/zhangnever/p/13419532.html
Copyright © 2011-2022 走看看