zoukankan      html  css  js  c++  java
  • Mongodb 学习笔记(三) .net core SDK

     首先添加 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); //删除数据

     

     

      

      

  • 相关阅读:
    HDU 1513 最长子序列
    HDU 3033 分组背包变形(每种至少一个)
    HDU 1712 分组背包
    深度学习
    《将博客搬至CSDN》
    UVa 10917 Dijkstra
    hdu 3839 Ancient Messages (dfs )
    LA 4255 UVa1423 拓扑排序
    poj 2515 差分序列,排列组合
    UVA 10054 the necklace 欧拉回路
  • 原文地址:https://www.cnblogs.com/jasonbourne3/p/11089372.html
Copyright © 2011-2022 走看看