开发学院

您的位置:首页>教程>正文

教程正文

MongoDB 查询文档

MongoDB 查询文档

  在本章中,我们将学习如何从MongoDB集合查询文档

使用find()方法

  要从MongoDB集合里查数据你需要使用find()方法.

语法

  find()方法的基本语法如下:

>db.COLLECTION_NAME.find()

  find()方法将以非结构化的方式显示所有文档。

使用pretty()方法

  若要以格式化的方式显示结果,您可以使用pretty()方法。

语法

>db.mycol.find().pretty()

例子

>db.mycol.find().pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "tutorials point",
   "url": "http://www.tutorialspoint.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>

  除了findOne()方法之外,还有findOne()方法,它只返回一个文档。

MongoDB中等效RDBMS的Where语法

  要根据某些条件查询文档,可以使用以下操作

Operation Syntax Example RDBMS Equivalent

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

MongoDB中的AND语法

语法

  在使用find()方法的时候,如果你想指定多个查询条件可以使用$and: 关键字来查询,多个条件使用“,”分隔,以此来实现类似RDBMS中的AND的功能,

>db.mycol.find(
   {
      $and: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

例子

  下面的例子查找作者为'tutorials point'和标题为 'MongoDB Overview'的所有数据

>db.mycol.find({$and:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty() {
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "tutorials point",
   "url": "http://www.tutorialspoint.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}

  上述代码实现了等效于RDBMS中的' where by = 'tutorials point' AND title = 'MongoDB Overview' '功能.

MongoDB中的OR

语法

  如果你想实现or的功能需要使用$or关键字,下面是例子:

>db.mycol.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

例子

  上述代码实现了查询作者为'tutorials point'或者标题为'MongoDB Overview功能.

>db.mycol.find({$or:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "tutorials point",
   "url": "http://www.tutorialspoint.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>

同时使用AND和OR

例子

  下面的例子将查询likes大于10并且标题为'MongoDB Overview'或者作者为'tutorials point'的记录.同等功能的SQL语句为:where likes>10 AND (by = 'tutorials point' OR title = 'MongoDB Overview')

>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"},
   {"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "tutorials point",
   "url": "http://www.tutorialspoint.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>