mongodb find()

Find查询是MongoDB中最基本也是最常用的语法。下面列出了Find的一些基本操作。对应sql
> 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的查询操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值