当前位置:Gxlcms > 数据库问题 > .net Core MongoDB用法演示

.net Core MongoDB用法演示

时间:2021-07-01 10:21:17 帮助过:19人阅读

1, name: "张三", age: 23,level:10, ename: { firstname: "san", lastname: "zhang"}, roles: ["vip","gen" ]}, {_id:2, name: "李四", age: 24,level:20, ename: { firstname: "si", lastname: "li"}, roles:[ "vip" ]}, {_id:3, name: "王五", age: 25,level:30, ename: { firstname: "wu", lastname: "wang"}, roles: ["gen","vip" ]}, {_id:4, name: "赵六", age: 26,level:40, ename: { firstname: "liu", lastname: "zhao"}, roles: ["gen"] }, {_id:5, name: "田七", age: 27, ename: { firstname: "qi", lastname: "tian"}, address:北京 }, {_id:6, name: "周八", age: 28,roles:["gen"], address:上海 } ])

1 添加(InsertOne,InsertMany)

  安装nuget包(或者执行命令  Install-Package MongoDB.Driver)

 技术图片

然后就可以使用C#操作MongoDB数据库了,看一个添加的demo:

  1. <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
  2. {
  3. </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
  4. {
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  6. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  8. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  9. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  10. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  11. </span><span style="color: #008000;">//</span><span style="color: #008000;">待添加的document</span>
  12. <span style="color: #0000ff;">var</span> doc = <span style="color: #0000ff;">new</span><span style="color: #000000;"> BsonDocument{
  13. { </span><span style="color: #800000;">"</span><span style="color: #800000;">_id</span><span style="color: #800000;">"</span>,<span style="color: #800080;">7</span><span style="color: #000000;"> },
  14. { </span><span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">吴九</span><span style="color: #800000;">"</span><span style="color: #000000;"> },
  15. { </span><span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">29</span><span style="color: #000000;"> },
  16. { </span><span style="color: #800000;">"</span><span style="color: #800000;">ename</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">new</span><span style="color: #000000;"> BsonDocument
  17. {
  18. { </span><span style="color: #800000;">"</span><span style="color: #800000;">firstname</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">jiu</span><span style="color: #800000;">"</span><span style="color: #000000;"> },
  19. { </span><span style="color: #800000;">"</span><span style="color: #800000;">lastname</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">wu</span><span style="color: #800000;">"</span><span style="color: #000000;"> }
  20. }
  21. }
  22. };
  23. </span><span style="color: #008000;">//</span><span style="color: #008000;">InsertOne()添加单条docment</span>
  24. <span style="color: #000000;"> collection.InsertOne(doc);
  25. }
  26. }</span>

执行完成后,查看在NoSQLBooster中查询,看到已经添加成功了:

 技术图片

我们也可以是用 collection.InsertMany(IEnumerable<BsonDocument> docs) 来批量添加docment,这里就不再演示了。

2  查询(Find,Filter,Sort,Projection)

1.简单查询(Find、Filter)

 查找name=“吴九"的记录

  1. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  2. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  3. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  4. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  5. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  6. </span><span style="color: #008000;">//</span><span style="color: #008000;">Fileter用于过滤,如查询name = 吴九的第一条记录</span>
  7. <span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
  8. </span><span style="color: #008000;">//</span><span style="color: #008000;">Find(filter)进行查询</span>
  9. <span style="color: #0000ff;">var</span> doc = collection.Find(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">吴九</span><span style="color: #800000;">"</span><span style="color: #000000;">)).FirstOrDefault();
  10. Console.WriteLine(doc);</span>

查询结果如下:

 技术图片

如果我们想查询所有的记录,可以设置过滤器为空,代码为: var docs = collection.Find(filter.Empty).ToList(); 

2.AND查询

查询年龄大于25且小于28的记录

  1. <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  2. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  3. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  4. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  6. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #008000;">//</span><span style="color: #008000;">查询25<age<28的记录</span>
  8. <span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
  9. </span><span style="color: #0000ff;">var</span> docs = collection.Find(filter.Gt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span>) & filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">28</span><span style="color: #000000;">)).ToList();
  10. docs.ForEach(d </span>=> Console.WriteLine(d));

 查询结果如下:

 技术图片

3 OR查询

查询年龄小于25或年龄大于28的记录

  1. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  2. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  3. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  4. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  5. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  6. </span><span style="color: #008000;">//</span><span style="color: #008000;">查询年龄小于25或年龄大于28的记录</span>
  7. <span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
  8. </span><span style="color: #0000ff;">var</span> docs = collection.Find(filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span>) | filter.Gt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">28</span><span style="color: #000000;">)).ToList();
  9. docs.ForEach(d </span>=> Console.WriteLine(d));

查询结果如下:

 技术图片

4 字段存在(Exists)

查询包含address字段的所有记录

  1. <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  2. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  3. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  4. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  6. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #008000;">//</span><span style="color: #008000;">查询存在address字段的记录</span>
  8. <span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
  9. </span><span style="color: #0000ff;">var</span> docs = collection.Find(filter.Exists(<span style="color: #800000;">"</span><span style="color: #800000;">address</span><span style="color: #800000;">"</span><span style="color: #000000;">)).ToList();
  10. docs.ForEach(d </span>=> Console.WriteLine(d));

查询结果如下:

 技术图片

5 排序(Sort)

查询年龄小于26岁的记录,并按年龄倒序排列

  1. <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  2. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  3. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  4. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  6. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #008000;">//</span><span style="color: #008000;">查询age<26的记录,按年龄倒序排列</span>
  8. <span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
  9. </span><span style="color: #0000ff;">var</span> sort = Builders<BsonDocument><span style="color: #000000;">.Sort;
  10. </span><span style="color: #0000ff;">var</span> docs = collection.Find(filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">26</span>))<span style="color: #008000;">//</span><span style="color: #008000;">过滤</span>
  11. .Sort(sort.Descending(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>)).ToList();<span style="color: #008000;">//</span><span style="color: #008000;">按age倒序</span>
  12. docs.ForEach(d => Console.WriteLine(d));

查询结果如下:

技术图片

6 查询指定字段(Projection)

MongoDB查询会默认返回_id字段,如果要不返回_id字段可以使用Exclude("_id")将其排除。

  1. <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  2. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  3. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  4. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  6. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #008000;">//</span><span style="color: #008000;">查询age<26的记录 包含name age 排除 _id</span>
  8. <span style="color: #0000ff;">var</span> project = Builders<BsonDocument><span style="color: #000000;">.Projection;
  9. </span><span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
  10. </span><span style="color: #0000ff;">var</span> docs = collection.Find(filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">26</span>))<span style="color: #008000;">//</span><span style="color: #008000;">过滤</span>
  11. .Project(project.Include(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>)<span style="color: #008000;">//</span><span style="color: #008000;">包含name</span>
  12. .Include(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>)<span style="color: #008000;">//</span><span style="color: #008000;">包含age</span>
  13. .Exclude(<span style="color: #800000;">"</span><span style="color: #800000;">_id</span><span style="color: #800000;">"</span>)<span style="color: #008000;">//</span><span style="color: #008000;">不包含_id</span>
  14. <span style="color: #000000;"> ).ToList();
  15. docs.ForEach(d </span>=> Console.WriteLine(d));

查询结果:

 技术图片

 3 修改(UpdateOne,UpdateMany)

1 修改单条记录(UpdateOne)

修改符合过滤条件的第一条记录,例子:将张三的年龄改成18岁

  1. <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  2. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  3. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  4. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  6. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
  8. </span><span style="color: #0000ff;">var</span> update = Builders<BsonDocument><span style="color: #000000;">.Update;
  9. </span><span style="color: #0000ff;">var</span> project = Builders<BsonDocument><span style="color: #000000;">.Projection;
  10. </span><span style="color: #008000;">//</span><span style="color: #008000;">将张三的年龄改成18</span>
  11. collection.UpdateOne(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span>), update.Set(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">18</span><span style="color: #000000;">));
  12. </span><span style="color: #008000;">//</span><span style="color: #008000;">查询张三的记录</span>
  13. <span style="color: #0000ff;">var</span> doc = collection.Find(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span><span style="color: #000000;">))
  14. .Project(project.Include(</span><span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span><span style="color: #000000;">))
  15. .FirstOrDefault();
  16. Console.WriteLine(doc);</span>

执行结果:

技术图片

2 修改多条记录(UpdateMany)

UpdateMany会修改所有符合过滤条件的记录,例子:将所有年龄小于25的记录标记为young(如果没有mark字段会自动添加)

  1. <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  2. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  3. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  4. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  6. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
  8. </span><span style="color: #0000ff;">var</span> update = Builders<BsonDocument><span style="color: #000000;">.Update;
  9. </span><span style="color: #0000ff;">var</span> project = Builders<BsonDocument><span style="color: #000000;">.Projection;
  10. </span><span style="color: #008000;">//</span><span style="color: #008000;">将张三的年龄改成18</span>
  11. collection.UpdateOne(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span>), update.Set(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">18</span><span style="color: #000000;">));
  12. </span><span style="color: #008000;">//</span><span style="color: #008000;">查询张三的记录</span>
  13. <span style="color: #0000ff;">var</span> doc = collection.Find(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span><span style="color: #000000;">))
  14. .Project(project.Include(</span><span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span><span style="color: #000000;">))
  15. .FirstOrDefault();
  16. Console.WriteLine(doc);</span>

查询结果如下,可以看到age<25的记录的mark字段值为young:

 技术图片

4 删除(DeleteOne和DeleteMany)

删除操作比较简单,DeleteOne用于删除符合过滤条件的第一条记录,DeleteMany用于删除所有符合过滤条件的记录。

1 删除单条记录(DeleteOne)

 删除名字为张三的记录

  1. <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  2. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  3. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  4. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  6. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
  8. </span><span style="color: #0000ff;">var</span> project = Builders<BsonDocument><span style="color: #000000;">.Projection;
  9. </span><span style="color: #008000;">//</span><span style="color: #008000;">删除名字为张三的记录</span>
  10. collection.DeleteOne(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span><span style="color: #000000;">));
  11. </span><span style="color: #0000ff;">var</span> docs =<span style="color: #000000;"> collection.Find(filter.Empty)
  12. .Project(project.Include(</span><span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">mark</span><span style="color: #800000;">"</span><span style="color: #000000;">))
  13. .ToList();
  14. docs.ForEach(d </span>=> Console.WriteLine(d));

执行结果如下,我们看到张三的记录已经被删除了:

技术图片

2  删除多条记录(DeleteMany)

删除所有年龄大于25岁的记录

  1. <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  2. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  3. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  4. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  6. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
  8. </span><span style="color: #0000ff;">var</span> project = Builders<BsonDocument><span style="color: #000000;">.Projection;
  9. </span><span style="color: #008000;">//</span><span style="color: #008000;">删除age>25的记录</span>
  10. DeleteResult result = collection.DeleteMany(filter.Gt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span><span style="color: #000000;">));
  11. Console.WriteLine($</span><span style="color: #800000;">"</span><span style="color: #800000;">一共删除了{result.DeletedCount}条记录</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  12. </span><span style="color: #0000ff;">var</span> docs =<span style="color: #000000;"> collection.Find(filter.Empty)
  13. .Project(project.Include(</span><span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">mark</span><span style="color: #800000;">"</span><span style="color: #000000;">))
  14. .ToList();
  15. docs.ForEach(d </span>=> Console.WriteLine(d));

执行结果如下,所有年龄大于25岁的记录都被删除了:

技术图片

5 类型映射

1 简单例子

 有时候我们要让查询的结果映射到我们的实体类中去,实现这个需求也十分简单,mongoDB支持自动映射,直接使用泛型即可,看一个(清空数据db.userinfos.remove({}),从新初始化js脚本):

  1. <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
  2. {
  3. </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
  4. {
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  6. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  8. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  9. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  10. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<Userinfo>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  11. </span><span style="color: #0000ff;">var</span> filter = Builders<Userinfo><span style="color: #000000;">.Filter;
  12. </span><span style="color: #0000ff;">var</span> sort = Builders<Userinfo><span style="color: #000000;">.Sort;
  13. List</span><Userinfo> userinfos = collection.Find(filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span>)) <span style="color: #008000;">//</span><span style="color: #008000;">查询年龄小于25岁的记录</span>
  14. .Sort(sort.Descending(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>)) <span style="color: #008000;">//</span><span style="color: #008000;">按年龄进行倒序</span>
  15. <span style="color: #000000;"> .ToList();
  16. </span><span style="color: #008000;">//</span><span style="color: #008000;">遍历结果</span>
  17. userinfos.ForEach(u => Console.WriteLine($<span style="color: #800000;">"</span><span style="color: #800000;">姓名:{u.name},年龄:{u.age},英文名:{u.ename.firstname} {u.ename.lastname}</span><span style="color: #800000;">"</span><span style="color: #000000;">));
  18. Console.ReadKey();
  19. }
  20. </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
  21. <span style="color: #808080;">///</span><span style="color: #008000;"> 用户类
  22. </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
  23. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Userinfo
  24. {
  25. </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> _id { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">id</span>
  26. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> name { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">姓名</span>
  27. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> age { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">年龄</span>
  28. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> level { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">等级</span>
  29. <span style="color: #0000ff;">public</span> Ename ename { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">英文名</span>
  30. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span>[] roles { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">角色</span>
  31. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> address { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">地址</span>
  32. <span style="color: #000000;"> }
  33. </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
  34. <span style="color: #808080;">///</span><span style="color: #008000;"> 英文名
  35. </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
  36. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Ename
  37. {
  38. </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> firstname { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
  39. </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> lastname { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
  40. }</span>


执行结果如下:

 技术图片

2 常用属性  

上边的例子仅仅用了基本的自动化映射,使用基本的自动化映射时:类和Bson中的字段必须严格一致(_id除外,可以自动映射到_id/id/Id),且Bson中的每一个字段在实体类中都必须有一个对应的字段,不然就会抛出异常,这就造成我们可能要写一个非常庞大的实体类,而且类中的字段命名也要严格和Bson中的字段一致。这些限制对我们开发来说是不能接受的,这里我们采用mongoDriver中的一些属性改进一下上边的代码,如下:

  1. <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
  2. {
  3. </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
  4. {
  5. </span><span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  6. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  7. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  8. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  9. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  10. <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<Userinfo>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  11. </span><span style="color: #0000ff;">var</span> filter = Builders<Userinfo><span style="color: #000000;">.Filter;
  12. </span><span style="color: #0000ff;">var</span> sort = Builders<Userinfo><span style="color: #000000;">.Sort;
  13. List</span><Userinfo> userinfos = collection.Find(filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span>)) <span style="color: #008000;">//</span><span style="color: #008000;">查询年龄小于25岁的记录</span>
  14. .Sort(sort.Descending(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>)) <span style="color: #008000;">//</span><span style="color: #008000;">按年龄进行倒序</span>
  15. <span style="color: #000000;"> .ToList();
  16. </span><span style="color: #008000;">//</span><span style="color: #008000;">遍历结果</span>
  17. userinfos.ForEach(u =><span style="color: #000000;">
  18. {
  19. Console.WriteLine($</span><span style="color: #800000;">"</span><span style="color: #800000;">编号:{u.userId},姓名:{u.name},年龄:{u.age},英文名:{u.ename?.ming} {u.ename?.xing},性别:{u.gender}</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  20. Console.WriteLine($</span><span style="color: #800000;">"</span><span style="color: #800000;">其他属性:{u.otherprops}</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  21. Console.WriteLine();
  22. });
  23. Console.ReadKey();
  24. }
  25. </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
  26. <span style="color: #808080;">///</span><span style="color: #008000;"> 用户类
  27. </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary
  28. </span><span style="color: #008000;"> //[BsonIgnoreExtraElements]</span>
  29. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Userinfo
  30. {
  31. [BsonId]
  32. </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> userId { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">id</span>
  33. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> name { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">姓名</span>
  34. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> age { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">年龄</span>
  35. <span style="color: #0000ff;">public</span> Ename ename { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">英文名</span>
  36. [BsonDefaultValue(<span style="color: #800000;">‘</span><span style="color: #800000;">男</span><span style="color: #800000;">‘</span><span style="color: #000000;">)]
  37. </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">char</span> gender { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
  38. [BsonIgnore]
  39. </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> nickname { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">昵称</span>
  40. <span style="color: #000000;"> [BsonExtraElements]
  41. </span><span style="color: #0000ff;">public</span> BsonDocument otherprops { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">其他属性</span>
  42. <span style="color: #000000;"> }
  43. </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
  44. <span style="color: #808080;">///</span><span style="color: #008000;"> 英文名
  45. </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
  46. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Ename
  47. {
  48. [BsonElement(</span><span style="color: #800000;">"</span><span style="color: #800000;">firstname</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
  49. </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> ming { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
  50. [BsonElement(</span><span style="color: #800000;">"</span><span style="color: #800000;">lastname</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
  51. </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> xing { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
  52. }</span>

执行结果如下:

 技术图片

这里用到了几个常用的属性,作用如下:

  BsonId修饰的字段对应BsonDocument中的_id;

  BsonDefaultValue(value)用于指定默认值;

  BsonIgnore表示不映射,即使BsonDocument中包含该字段也不会赋值给属性;

  BsonExtraElements修饰的字段用于存储没有映射到类中的其他属性;

  BsonElement可以指定修饰的属性映射到BsonDocument中的哪个字段,

还有一些其他的属性,具体可以参考官方文档。

3 MongoDB使用Linq查询 

 MongoDB的驱动支持Linq查询,用法十分简单,引用  usingMongoDB.Driver.Linq;  后,使用  collection.AsQueryable()  获取IMongoQueryable<T>实例,然后就可以使用Linq对这个IMongoQueryable<T>进行查询了。

  使用过EF的小伙伴应该都了解IQueryable+Linq的查询默认不是将整个结果集立即加载到内存中,而是延迟加载的,即只有使用结果时(如 在执行First,Last,Single,Count,ToList等)才会将Linq转换成Sql,在数据库中执行查询操作。类似的,在使用IMongoQueryable<T>+Linq进行查询时默认也是延迟加载的,在需要使用查询结果时,驱动会将Linq转换成聚合管道命令(aggregation pipeline),如Linq中的Where被转换成$watch,Join转换为$lookup,Skip和Take转换为$skip和$limit等等,然后在Mongodb中执行这些管道命令获取结果,看一个栗子我们就会轻松地掌握了。

首先添加一些测试数据:

  1. <span style="color: #008000;">//</span><span style="color: #008000;">添加学生数据</span>
  2. <span style="color: #000000;">db.students.insertMany([
  3. {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span>:<span style="color: #800080;">1</span>, <span style="color: #800000;">"</span><span style="color: #800000;">stuName</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">jack</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>:<span style="color: #800080;">23</span>, <span style="color: #800000;">"</span><span style="color: #800000;">classNo</span><span style="color: #800000;">"</span>:<span style="color: #800080;">1</span><span style="color: #000000;">},
  4. {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span>:<span style="color: #800080;">2</span>, <span style="color: #800000;">"</span><span style="color: #800000;">stuName</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">tom</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>:<span style="color: #800080;">20</span>, <span style="color: #800000;">"</span><span style="color: #800000;">classNo</span><span style="color: #800000;">"</span>:<span style="color: #800080;">2</span><span style="color: #000000;">},
  5. {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span>:<span style="color: #800080;">3</span>, <span style="color: #800000;">"</span><span style="color: #800000;">stuName</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">hanmeimei</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>:<span style="color: #800080;">22</span>, <span style="color: #800000;">"</span><span style="color: #800000;">classNo</span><span style="color: #800000;">"</span>:<span style="color: #800080;">1</span><span style="color: #000000;">},
  6. {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span>:<span style="color: #800080;">4</span>, <span style="color: #800000;">"</span><span style="color: #800000;">stuName</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">lilei</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>:<span style="color: #800080;">24</span>, <span style="color: #800000;">"</span><span style="color: #800000;">classNo</span><span style="color: #800000;">"</span>:<span style="color: #800080;">2</span><span style="color: #000000;">}
  7. ])
  8. </span><span style="color: #008000;">//</span><span style="color: #008000;">添加班级数据</span>
  9. <span style="color: #000000;">db.classes.insertMany([
  10. {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span> : <span style="color: #800080;">1</span>,<span style="color: #800000;">"</span><span style="color: #800000;">clsName</span><span style="color: #800000;">"</span> : <span style="color: #800000;">"</span><span style="color: #800000;">A班</span><span style="color: #800000;">"</span><span style="color: #000000;">},
  11. {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span> : <span style="color: #800080;">2</span>,<span style="color: #800000;">"</span><span style="color: #800000;">clsName</span><span style="color: #800000;">"</span> : <span style="color: #800000;">"</span><span style="color: #800000;">B班</span><span style="color: #800000;">"</span><span style="color: #000000;">}
  12. ])</span>

这里查找了两组数据:①基本查询:查找年龄大于22岁的学生;②连接查询:查询各个学生的学号、姓名、班级名。例子比较简单,直接看代码吧

  1. <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ConsoleAppCore
  2. {
  3. </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System;
  4. </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> MongoDB.Bson;
  5. </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> MongoDB.Bson.Serialization.Attributes;
  6. </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> MongoDB.Driver;
  7. </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> MongoDB.Driver.Linq;
  8. </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Collections.Generic;
  9. </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
  10. {
  11. </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
  12. {
  13. </span><span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
  14. <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  15. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
  16. <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  17. </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
  18. <span style="color: #0000ff;">var</span> stuCollection = mydb.GetCollection<Student>(<span style="color: #800000;">"</span><span style="color: #800000;">students</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  19. </span><span style="color: #0000ff;">var</span> clsCollection = mydb.GetCollection<Classx>(<span style="color: #800000;">"</span><span style="color: #800000;">classes</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  20. </span><span style="color: #008000;">//</span><span style="color: #008000;">查找年龄大于22的学生</span>
  21. Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">-------------查找年龄大于22的学生列表--------------</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  22. </span><span style="color: #008000;">//</span><span style="color: #008000;">1.query语法</span>
  23. List<Student> stuList1 = (<span style="color: #0000ff;">from</span> stu <span style="color: #0000ff;">in</span><span style="color: #000000;"> stuCollection.AsQueryable()
  24. </span><span style="color: #0000ff;">where</span> stu.age > <span style="color: #800080;">22</span>
  25. <span style="color: #0000ff;">select</span><span style="color: #000000;"> stu).ToList();
  26. </span><span style="color: #008000;">//</span><span style="color: #008000;">2.点语法</span>
  27. List<Student> stuList2 = stuCollection.AsQueryable().Where(s => s.age > <span style="color: #800080;">22</span><span style="color: #000000;">).ToList();
  28. stuList1.ForEach(stu </span>=> Console.WriteLine($<span style="color: #800000;">"</span><span style="color: #800000;">姓名:{stu?.stuName}, 年龄:{stu?.age}</span><span style="color: #800000;">"</span><span style="color: #000000;">));
  29. Console.WriteLine();
  30. </span><span style="color: #008000;">//</span><span style="color: #008000;">表连接查询,查询各个学生的班级名</span>
  31. Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">-------------表连接,查询学生的班级名----------------</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  32. </span><span style="color: #008000;">//</span><span style="color: #008000;">1.query语法</span>
  33. <span style="color: #0000ff;">var</span> result1 = <span style="color: #0000ff;">from</span> stu <span style="color: #0000ff;">in</span><span style="color: #000000;"> stuCollection.AsQueryable()
  34. join cls </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> clsCollection.AsQueryable()
  35. on stu.classNo equals cls.no
  36. </span><span style="color: #0000ff;">select</span> <span style="color: #0000ff;">new</span> { stuno =<span style="color: #000000;"> stu.no, stu.stuName, cls.clsName };
  37. </span><span style="color: #008000;">//</span><span style="color: #008000;">2.点语法</span>
  38. <span style="color: #0000ff;">var</span> result2 =<span style="color: #000000;"> stuCollection.AsQueryable().Join(
  39. clsCollection.AsQueryable(),
  40. stu </span>=><span style="color: #000000;"> stu.classNo,
  41. cls </span>=><span style="color: #000000;"> cls.no,
  42. (stu, cls) </span>=> <span style="color: #0000ff;">new</span> { stuno =<span style="color: #000000;"> stu.no, stu.stuName, cls.clsName }
  43. );
  44. </span><span style="color: #008000;">//</span><span style="color: #008000;">遍历结果</span>
  45. <span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> item <span style="color: #0000ff;">in</span><span style="color: #000000;"> result1)
  46. {
  47. Console.WriteLine($</span><span style="color: #800000;">"</span><span style="color: #800000;">学号:{item.stuno}, 姓名:{item.stuName}, 班级:{item.clsName}</span><span style="color: #800000;">"</span><span style="color: #000000;">);
  48. }
  49. Console.ReadKey();
  50. }
  51. </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
  52. <span style="color: #808080;">///</span><span style="color: #008000;"> 学生类
  53. </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary
  54. </span><span style="color: #008000;"> public class Student</span>
  55. <span style="color: #000000;"> {
  56. </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> no { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">学号</span>
  57. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> stuName { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">姓名</span>
  58. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> age { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">年龄</span>
  59. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> classNo { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">班级编号</span>
  60. <span style="color: #000000;"> [BsonExtraElements]
  61. </span><span style="color: #0000ff;">public</span> BsonDocument others { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
  62. }
  63. </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
  64. <span style="color: #808080;">///</span><span style="color: #008000;"> 班级类
  65. </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
  66. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Classx
  67. {
  68. </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> no { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">班级编号</span>
  69. <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> clsName { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">班级名</span>
  70. <span style="color: #000000;"> [BsonExtraElements]
  71. </span><span style="color: #0000ff;">public</span> BsonDocument others { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
  72. }
  73. }
  74. }</span>

执行结果如下:

技术图片

.net Core MongoDB用法演示

标签:查询   简单例子   clu   nosql   ESS   mamicode   命名   nbsp   project   

人气教程排行