Mongodb查询文档

Mongodb查询文档

> db.blog.find()     --查看集合下的所有文档

{ "_id" :ObjectId("5438dd3fa7ccb1d4ecc7571d"), "title" : "myblog post", "conte

nt" : "here is blog past","date" : ISODate("2014-10-11T07:32:55.652Z")}

{ "_id" :ObjectId("5438df80a7ccb1d4ecc7571e"), "name" : "blog","author" : "wan

gwei" }

db.blog.find({}).limit(50); 可以限制查询文档的数量

> db.blog.find({}).limit(1);

{ "_id" :ObjectId("5438dd3fa7ccb1d4ecc7571d"), "title" : "myblog post", "conte

nt" : "here is blog past","date" : ISODate("2014-10-11T07:32:55.652Z")}

> db.blog.find().pretty();  ---格式化查询文档加上pretty()

{

       "_id" : ObjectId("5438dd3fa7ccb1d4ecc7571d"),

       "title" : "my blog post",

       "content" : "here is blog past",

       "date" : ISODate("2014-10-11T07:32:55.652Z")

}

{

       "_id" : ObjectId("5438df80a7ccb1d4ecc7571e"),

       "name" : "blog",

       "author" : "wangwei"

}

> db.blog.findOne()  ----查看集合下一个文档

{

       "_id" : ObjectId("5438dd3fa7ccb1d4ecc7571d"),

       "title" : "my blog post",

       "content" : "here is blog past",

       "date" : ISODate("2014-10-11T07:32:55.652Z")

}

> db.blog.find({ "name" :"blog" }).limit(50).pretty()  --按条件查询文档

{

       "_id" : ObjectId("5438df80a7ccb1d4ecc7571e"),

       "name" : "blog",

       "author" : "wangwei"

}

对于上面给出的例子相当于where子句 where name= ‘blog’

Mongodb中的and

> db.blog.find({ "name" :"blog", "author" : "wangwei" }).limit(50);

{ "_id" :ObjectId("5438df80a7ccb1d4ecc7571e"), "name" :"blog", "author" : "wan

gwei" }

> db.blog.find({ "name" :"blog", "author" : "wangweidd" }).limit(50);

> --返回为空

对于上面给出的例子相当于where子句 where name= ‘blog’ and author =’wangwei’

 

Mongodb中的or

> db.blog.find({ "$or" : [{"name" : "blog" }, { "title" : "my blogpost" }] }).

limit(50).pretty()

{

       "_id" : ObjectId("5438dd3fa7ccb1d4ecc7571d"),

       "title" : "my blog post",

       "content" : "here is blog past",

       "date" : ISODate("2014-10-11T07:32:55.652Z")

}

{

       "_id" : ObjectId("5438df80a7ccb1d4ecc7571e"),

       "name" : "blog",

       "author" : "wangwei"

}

 

对于上面给出的例子相当于where子句 where name= ‘blog’ or title =’ myblog post’

 

> db.blog.find({ "name" :"blog" }, { "name" : 1 }).limit(50).pretty();  --查询显示特定列(字段)

{ "_id" :ObjectId("5438df80a7ccb1d4ecc7571e"), "name" :"blog" }

对于上面给出的例子相当于 select name from

 

> db.blog.find({ "name" :"blog" }, { "name" : 1 }).limit(50).sort({ "name": -

 });    --按字段排序查询

{ "_id" :ObjectId("5438df80a7ccb1d4ecc7571e"), "name" :"blog" }

sort() 方法接受一个文档,其中包含的字段列表连同他们的排序顺序。要指定排序顺序1-1 1用于升序排列,而-1用于降序。

RDBMS Where子句和MongoDB等同语句

要查询文件的一些条件的基础上,可以使用下面的操作 www.yiibai.com

操作

语法

例子

RDBMS 等同

Equality

{<key>:<value>}

db.mycol.find({"by":"tutorials point"}).pretty()

where by = 'tutorials point'

Less Than

{<key>:{$lt:<value>}}

db.mycol.find({"likes":{$lt:50}}).pretty()

where likes < 50

Less Than Equals

{<key>:{$lte:<value>}}

db.mycol.find({"likes":{$lte:50}}).pretty()

where likes <= 50

Greater Than

{<key>:{$gt:<value>}}

db.mycol.find({"likes":{$gt:50}}).pretty()

where likes > 50

Greater Than Equals

{<key>:{$gte:<value>}}

db.mycol.find({"likes":{$gte:50}}).pretty()

where likes >= 50

Not Equals

{<key>:{$ne:<value>}}

db.mycol.find({"likes":{$ne:50}}).pretty()

where likes != 50

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值