MongoDB tutoria...login
MongoDB tutorial
author:php.cn  update time:2022-04-21 17:49:03

MongoDB query document


Syntax

The syntax format of MongoDB query data is as follows:

>db.COLLECTION_NAME.find()

The find() method displays all documents in an unstructured way.

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

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

The pretty() method displays all documents in a formatted way. .

Example

In the following example we query the data in the collection col:

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

In addition to the find() method, there is also a findOne() method, which only returns a document.


Comparison of MongoDB and RDBMS Where statements

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

## is less than ##db.col.find({"likes":{$lt:50}}). pretty(){< key>:{$lte:<value>}}{<key>:{$gt :<value>}}{<key>:{$gte:<value> }}##db.col.find({"likes":{$gte:50}}).pretty()where likes >= 50# is not equal to

MongoDB AND condition

MongoDB's find() method can pass in multiple keys (keys), each key (key) separated by commas, and the AND condition of regular SQL.

The syntax format is as follows:

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

Example

The following example uses the by and title keys to query php Chinese Data from MongoDB Tutorial in Net

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

The above example is similar to the WHERE statement: WHERE by='php中文网' AND title='MongoDB Tutorial'


MongoDB OR condition

MongoDB OR conditional statement uses the keyword $or, and the syntax format is as follows:

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

Example

In the following example, we demonstrate the query key by The value is php Chinese website or the key title The value is MongoDB Tutorial Document.

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

Combined use of AND and OR

The following example demonstrates the joint use of AND and OR, similar to the conventional SQL statement: 'where likes>50 AND (by = ' php Chinese website' OR title = 'MongoDB Tutorial')'

>db.col.find({"likes": {$gt:50}, $or: [{"by": "php中文网"},{"title": "MongoDB 教程"}]}).pretty()
{
        "_id" : ObjectId("56063f17ade2f21f36b03133"),
        "title" : "MongoDB 教程",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "php中文网",
        "url" : "http://www.php.cn",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}
OperationFormatExampleSimilar statements in RDBMS
equals{<key>:<value>}db.col.find({"by":"php中文网"}).pretty()where by = 'php中文网'
{<key>:{$lt:<value>}}##where likes < 50##is less than or equal to
db.col.find({"likes":{$lte:50}}).pretty()where likes <= 50 is greater than
##db.col.find({"likes":{$gt:50}}).pretty()where likes > 50greater than or equal to
{<key>:{$ne:<value>}}
db.col.find({"likes":{$ne:50}}).pretty()where likes != 50