MongoDB 查询文档
语法
基本的find()方法语法如下
>db.COLLECTION_NAME.find()
pretty() 方法
结果显示在一个格式化的方式,可以使用 pretty() 方法.
语法:
>db.mycol.find().pretty()
例子
> db.mycol.find().pretty()
{
"_id" : ObjectId("5799c3eb235910620b89c674"),
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials itcast",
"url" : "http://www.itcast.cn",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
{
"_id" : ObjectId("5799c3f3235910620b89c675"),
"title" : "MySQL Overview",
"description" : "MySQL is sql database",
"by" : "tutorials itcast",
"url" : "http://www.itcast.cn",
"tags" : [
"MySQL",
"database",
"SQL"
],
"likes" : 40
}
>
除了find() 方法外,还有一个 findOne() 法,返回一个结果。
db.mycol.findOne()
MongoDB 与 RDBMS Where 语句比较
操作 | 语法 | 例子 | RDBMS 等同 |
---|---|---|---|
Equality | {<key>:<value>} | db.mycol.find({"by":"tutorials itcast"}).pretty() | where by = 'tutorials itcast' |
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 |
AND 在MongoDB中用法
语法:
在 find() 方法,如果通过多个键分离',',那么 MongoDB 处理 AND 条件。AND 基本语法如下所示:
>db.mycol.find({key1:value1, key2:value2}).pretty()
例子
下面给出的例子将显示所有的教程,标题是“MongoDB Overview“
> db.mycol.find({"by":"tutorials itcast","title": "MongoDB Overview"}).pretty()
{
"_id" : ObjectId("5799c3eb235910620b89c674"),
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials itcast",
"url" : "http://www.itcast.cn",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>
对于上面给出的例子相当于where子句 where by='tutorials itcast' AND title='MongoDB Overview'
,可以通过任意数量的键值对在 find 子句。
MongoDB中OR
语法:
OR条件的基础上要查询文件,需要使用$or关键字。OR 基本语法如下所示:
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
例子
下面给出的例子将显示所有的教程,标题是“MongoDB Overview'或'MySQL Overview'
>db.mycol.find({
$or: [
{
"title": "MySQL Overview"
},
{
"title": "MongoDB Overview"
}
]
}).pretty()
{
"_id" : ObjectId("57cefded600d5bb1e2acdb70"),
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials itcast",
"url" : "http://www.itcast.cn",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
{
"_id" : ObjectId("57cefdf7600d5bb1e2acdb71"),
"title" : "MySQL Overview",
"description" : "MySQL is sql database",
"by" : "tutorials itcast",
"url" : "http://www.itcast.cn",
"tags" : [
"MySQL",
"database",
"SQL"
],
"likes" : 40
}
>
AND 和 OR 一起使用
例子
下面给出的例子将显示 喜爱数高于50,其标题是MongoDB Overview
或者是itcast
所写 。等效于 SQL where子句 为 where likes>10 AND (by = 'itcast' OR title = 'MongoDB Overview')
> db.mycol.find({
"likes": {
$gt: 50
},
$or: [
{
"by": "tutorials itcast"
},
{
"title": "MongoDB Overview"
}
]
}).pretty()
{
"_id" : ObjectId("57cefded600d5bb1e2acdb70"),
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials itcast",
"url" : "http://www.itcast.cn",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>
当前内容版权归 piaosanlang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 piaosanlang .