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 |