MongoDB 之 Find查詢

來源:互聯網
上載者:User
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的查詢操作。MongoDB Find查詢  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);//

如果要看更多,請訪問我之前的MongoDb系列文章作者: Yoolo出處:http://www.cnblogs.com/yoolonet/archive/2011/09/06/2168447.html

本文著作權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連. 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.