MongoDB 查询文档
pretty() 方法
结果显示在一个格式化的方式,可以使用 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
>
MongoDB 与 RDBMS Where 语句比较
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",
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>
MongoDB中OR
语法:
OR条件的基础上要查询文件,需要使用$or关键字。OR 基本语法如下所示:
例子
下面给出的例子将显示所有的教程,标题是“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",
"tags" : [
"MySQL",
"database",
"SQL"
],
"likes" : 40
}
>
AND 和 OR 一起使用
例子
> 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
>