Mongodb 学习笔记(三) .net core SDK

栏目: ASP.NET · 发布时间: 4年前

内容简介:首先添加 Nuget包  MongoDB.Driver创建一个Model。与Mongodb建立连接:

首先添加 Nuget包  MongoDB.Driver

创建一个Model。

public class Student {

    public ObjectId _id { get; set; }
    public string name { get; set; }
    public int age { get; set; }
    public Address address { get; set; }
}
public class Address{
    public string province { get; set; }
    public string city { get; set; }
}

Mongodb 建立连接:

 var client = new MongoClient("mongodb://127.0.0.1:27017");   //与Mongodb建立连接。
 var db = client.GetDatabase("test");  //获取数据库
 var collection = db.GetCollection<BsonDocument>("student");   //选择操作集合

增删改查:

 1  Student student = new Student()
 2  {
 3    name = "lilie",
 4    age = 11,
 5    address = new Address() { province = "GuangDong",city "shenzhen"}
 6  };
 7  
 8  collection.InsertOne(student.ToBsonDocument()); //插入数据
 9  var filter = Builders<BsonDocument>.Filter.Eq("name", "lilie"); //声明过滤条件
10  var list = collection.Find(filter).As<Student>().ToList();  //查询数据
11  collection.UpdateOne(filter,Builders<BsonDocument>.Update.Set("age", "24")); //更新数据
12  collection.DeleteOne(filter); //删除数据


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Designing Programmes

Designing Programmes

Karl Gerstner / Springer Verlag / 2007 / $499.00

Karl Gerstnera (TM)s work is a milestone in the history of design. One of his most important works is Designing Programmes, which is presented here in a new edition of the original 1964 publication. I......一起来看看 《Designing Programmes》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具