3.16. MongoDB query document

发布时间 :2025-10-25 12:33:00 UTC      

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
}

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.