Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MongoDB queries the documentation


May 17, 2021 MongoDB


Table of contents


MongoDB queries the documentation

Grammar

The syntax format for MongoDB query data is as follows:

>db.COLLECTION_NAME.find()

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

If you need to read the data in an easy-to-read way, you can use the pretty() method in the following syntax format:

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

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

Instance

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

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

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


MongoDB is compared to the RDBMS Where statement

If you are familiar with regular SQL data, the following table gives you a better understanding of MongoDB's conditional statement queries:

Operation Format Example A similar statement in RDBMS
Equals {<key>:<value> } db.col.find({"by":"w3cschool"}).pretty() where by = 'w3cschool'
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
Is 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() method can pass in multiple keys (keys), each separated by a comma, and the AND condition of regular SQL.

The syntax format is as follows:

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

Instance

The following example queries the data for the MongoDB tutorial in w3cschool with the by and title keys

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

The above example is similar to the WHERE statement: WHERE by 'w3cschool' AND title 'MongoDB Tutorial'


MongoDB OR condition

The MongoDB OR conditional statement uses the keyword $or, in the following syntax format:

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

Instance

In the following example, we demonstrated a document with a query key by value of w3cschool or a key title value of MongoDB tutorial.

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

AND and OR are used in union

The following example demonstrates the joint use of AND and OR, similar to the general SQL statement: 'Where likes sgt;50 AND (by s'w3cschool' OR title s'MongoDB tutorial').

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