MongoDB 之 Find查询
Find查询是MongoDB中最基本也是最常用的语法。使用起来也非常简单。下面列出了Find的一些基本操作。
> db.news.find() //select * from [news]
{ "_id" : 10001, "count" : 1, "news" : "i hava a dream", "time" : ISODate("2011-09-05T13:40:58.034Z") }
{ "_id" : 10002, "count" : 2, "news" : "wow! it is very good!", "time" : ISODate("2011-09-05T13:41:57.860Z") }
{ "_id" : 10003, "count" : 3, "news" : "i hava a dream", "time" : ISODate("2011-09-05T13:42:09.794Z") }
{ "_id" : 10004, "count" : 4, "news" : "wow! it is very good!", "time" : ISODate("2011-09-05T13:42:19.185Z") }
{ "_id" : 10005, "count" : 5, "news" : "new a good day", "time" : ISODate("2011-09-05T13:42:36.860Z") }
> db.news.findOne() //select top 1 * from [news]
{ "_id" : 10001, "count" : 1, "news" : "i hava a dream", "time" : ISODate("2011-09-05T13:40:58.034Z") }
> db.news.find({"_id":10001,"count":1}) //select * from [news] where _id=10001 and count=1
{ "_id" : 10001, "count" : 1, "news" : "i hava a dream", "time" : ISODate("2011-09-05T13:40:58.034Z") }
> db.news.find({"count":{"$gt":2,"$lte":4}}) //select * from [news] where count>2 and count<=4
{ "_id" : 10003, "count" : 3, "news" : "i hava a dream", "time" : ISODate("2011-09-05T13:42:09.794Z") }
{ "_id" : 10004, "count" : 4, "news" : "wow! it is very good!", "time" : ISODate("2011-09-05T13:42:19.185Z") }
> stime = new Date()
ISODate("2011-09-05T13:57:21.812Z")
> db.news.find({"time":{"$lt":stime}}) //select * from news where time<ISODate("2011-09-05T13:57:21.812Z")
{ "_id" : 10001, "count" : 1, "news" : "i hava a dream", "time" : ISODate("2011-09-05T13:40:58.034Z") }
{ "_id" : 10002, "count" : 2, "news" : "wow! it is very good!", "time" : ISODate("2011-09-05T13:41:57.860Z") }
{ "_id" : 10003, "count" : 3, "news" : "i hava a dream", "time" : ISODate("2011-09-05T13:42:09.794Z") }
{ "_id" : 10004, "count" : 4, "news" : "wow! it is very good!", "time" : ISODate("2011-09-05T13:42:19.185Z") }
{ "_id" : 10005, "count" : 5, "news" : "new a good day", "time" : ISODate("2011-09-05T13:42:36.860Z") }
> db.news.find({"count":{"$gt":2,"$lte":4}},{"_id":0,"time":1,"news":1}) // select time,news from [news] where count>2 and count<=4
{ "news" : "i hava a dream", "time" : ISODate("2011-09-05T13:42:09.794Z") }
{ "news" : "wow! it is very good!", "time" : ISODate("2011-09-05T13:42:19.185Z") }
下面是对应的C#代码
FindAllAs<TDocument>()是一个泛型方法,可以用原生态BsonDocument和自定义类型News作为类型参数。MongoCursor<TDocument> FindAs<TDocument>(IMongoQuery query) 泛型查找,MongoCursor FindAs(Type, IMongoQuery)指定Type类型,并且两个方法都返回游标。而且MongoDB也提供Linq的查询操作。

1 public class News
2 {
3 public int _id { get; set; }
4 public int count { get; set; }
5 public string news { get; set; }
6 public DateTime time { get; set; }
7 }
8
9 MongoCursor<BsonDocument> allDoc = coll.FindAllAs<BsonDocument>();
10 BsonDocument doc = allDoc.First(); //BsonDocument类型参数
11
12 MongoCursor<News> allNews = coll.FindAllAs<News>();
13 News aNew = allNews.First(); //News类型参数
14
15 News firstNews = coll.FindOneAs<News>(); //查找第一个文档
16
17 QueryDocument query = new QueryDocument(); //定义查询文档
18 query.Add("_id", 10001);
19 query.Add("count", 1);
20 MongoCursor<News> qNews = coll.FindAs<News>(query);
21
22
23 BsonDocument bd = new BsonDocument();//定义查询文档 count>2 and count<=4
24 bd.Add("$gt", 2);
25 bd.Add("$lte", 4);
26 QueryDocument query_a = new QueryDocument();
27 query_a.Add("count",bd);
28
29 FieldsDocument fd = new FieldsDocument();
30 fd.Add("_id", 0);
31 fd.Add("count", 1);
32 fd.Add("time", 1);
33
34 MongoCursor<News> mNewss = coll.FindAs<News>(query_a).SetFields(fd);//只返回count和time
35
36 var time = BsonDateTime.Create("2011/9/5 23:26:00");
37 BsonDocument db_t = new BsonDocument();
38 db_t.Add("$gt", time);
39 QueryDocument qd_3 = new QueryDocument();
40 qd_3.Add("time", db_t);
41
42 MongoCursor<News> mNews = coll.FindAs<News>(qd_3);//
2 {
3 public int _id { get; set; }
4 public int count { get; set; }
5 public string news { get; set; }
6 public DateTime time { get; set; }
7 }
8
9 MongoCursor<BsonDocument> allDoc = coll.FindAllAs<BsonDocument>();
10 BsonDocument doc = allDoc.First(); //BsonDocument类型参数
11
12 MongoCursor<News> allNews = coll.FindAllAs<News>();
13 News aNew = allNews.First(); //News类型参数
14
15 News firstNews = coll.FindOneAs<News>(); //查找第一个文档
16
17 QueryDocument query = new QueryDocument(); //定义查询文档
18 query.Add("_id", 10001);
19 query.Add("count", 1);
20 MongoCursor<News> qNews = coll.FindAs<News>(query);
21
22
23 BsonDocument bd = new BsonDocument();//定义查询文档 count>2 and count<=4
24 bd.Add("$gt", 2);
25 bd.Add("$lte", 4);
26 QueryDocument query_a = new QueryDocument();
27 query_a.Add("count",bd);
28
29 FieldsDocument fd = new FieldsDocument();
30 fd.Add("_id", 0);
31 fd.Add("count", 1);
32 fd.Add("time", 1);
33
34 MongoCursor<News> mNewss = coll.FindAs<News>(query_a).SetFields(fd);//只返回count和time
35
36 var time = BsonDateTime.Create("2011/9/5 23:26:00");
37 BsonDocument db_t = new BsonDocument();
38 db_t.Add("$gt", time);
39 QueryDocument qd_3 = new QueryDocument();
40 qd_3.Add("time", db_t);
41
42 MongoCursor<News> mNews = coll.FindAs<News>(qd_3);//
如果要看更多,请访问我之前的MongoDb系列文章
作者: Yoolo
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述