Find query is the most basic and commonly used syntax in MongoDB. It is also very easy to use. Some basic operations of find are listed below.> 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 ")} the corresponding C # code findallas <tdocument> () is a generic method, you can use the original ecological bsondocument and custom type news as the type parameter. Extends cursor <tdocument> findas <tdocument> (imongoquery query) generic search, extends cursor findas (type, imongoquery) specifies the type, and both methods return the cursor. In addition, MongoDB also provides the query operation of LINQ. MongoDB find query 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 Export cursor <bsondocument> alldoc = Coll. findallas <bsondocument> ();
10 bsondocument Doc = alldoc. First (); // bsondocument type parameter
11
12 Export cursor <News> allnews = Coll. findallas <News> ();
13 News anew = allnews. First (); // news type parameter
14
15 news firstnews = Coll. findoneas <News> (); // find the first document
16
17 querydocument query = new querydocument (); // defines the query document
18 query. Add ("_ id", 10001 );
19 query. Add ("count", 1 );
20 export cursor <News> qnews = Coll. findas <News> (query );
21
22
23 bsondocument BD = new bsondocument (); // defines the query document 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 response cursor <News> mnewss = Coll. findas <News> (query_a). setfields (FD); // only count and time are returned.
35
36 var time = bsondatetime. Create ("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 export cursor <News> mnews = Coll. findas <News> (qd_3 );//
If you want to see more, visit my previous MongoDB series Author: yoolo Source: http://www.cnblogs.com/yoolonet/archive/2011/09/06/2168447.html
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent and provide the original article connection clearly on the article page.