3.16. MongoDB query document

发布时间 : 2025-10-25 13:34:23 UTC      

Page Views: 9 views

MongoDB query document usage find() Method.

find() Method to display all documents in an unstructured way.

3.16.1. Grammar

The syntax format of MongoDB query data is as follows:

db.collection.find(query, projection) 
  • query Optional, use the query operator to specify query conditions

  • projection Optionally, use the projection operator to specify the returned key When querying, all the key values in the document are returned, simply omitting the parameter (default omitted).

If you need to read data in an easy-to-read way, you can use pretty() Method, the syntax format is as follows:

>db.col.find().pretty() 

pretty() Method to display all documents in a formatted manner.

3.16.2. Example

For the following example, we queried the data in the collection col:

> db.col.find().pretty() { "_id" : ObjectId("56063f17ade2f21f36b03133"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } 

Except find() In addition to the method, there is another one. findOne() Method, which returns only one document.

Comparison of MongoDB and RDBMS Where statements

If you are familiar with regular SQL data, you can better understand MongoDB conditional queries through the following table:

Operation

Format

Example

Similar statements in RDBMS

Equal to

{< key >: < value >}

Db.col.find ({“by”: “Rookie tutorial”}) .pretty ()

Where by = ‘rookie tutorial’

Less than

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

Db.col.find ({“likes”: {$lt:50}}) .pretty ()

Where likes < 50

Less than or equal to

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

Db.col.find ({“likes”: {$lte:50}}) .pretty ()

Where likes < = 50

Greater than

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

Db.col.find ({“likes”: {$gt:50}}) .pretty ()

Where likes > 50

Greater than or equal to

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

Db.col.find ({“likes”: {$gte:50}}) .pretty ()

Where likes > = 50

Not equal to

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

Db.col.find ({“likes”: {$ne:50}}) .pretty ()

Where likes! = 50

MongoDB AND condition

MongoDB’s find() A method can pass in multiple keys (key), each key separated by a comma, that is, the AND condition of a regular SQL.

The syntax format is as follows:

>db.col.find({key1:value1, key2:value2}).pretty() 

3.16.3. Example

The following example is passed through the by And title Key to query 菜鸟教程 Medium MongoDB 教程 Data of

> db.col.find({"by":"菜鸟教程", "title":"MongoDB 教程"}).pretty() { "_id" : ObjectId("56063f17ade2f21f36b03133"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } 

In the above example, it is similar to WHERE Statement: WHERE by=’菜鸟教程’ AND title=’MongoDB 教程’

MongoDB OR condition

The MongoDB OR conditional statement uses keywords $or The syntax format is as follows:

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

3.16.4. Example

In the following example, we demonstrate the query key by Documents with values of rookie tutorials or key title values of MongoDB tutorials.

>db.col.find({$or:[{"by":"菜鸟教程"},{"title": "MongoDB 教程"}]}).pretty() { "_id" : ObjectId("56063f17ade2f21f36b03133"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } > 

Combined use of AND and OR

The following example demonstrates the combined use of AND and OR, similar to the regular SQL statement: ‘where likes > 50 AND (by =’ rookie tutorial’OR title = ‘MongoDB tutorial’)’

>db.col.find({"likes": {$gt:50}, $or: [{"by": "菜鸟教程"},{"title": "MongoDB 教程"}]}).pretty() { "_id" : ObjectId("56063f17ade2f21f36b03133"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。