Latest web development tutorials

MongoDB query document

grammar

MongoDB query data syntax is as follows:

>db.COLLECTION_NAME.find()

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

If you need to be readable way to read the data, you can use pretty () method syntax is as follows:

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

pretty () method to format the way to display all documents.

Examples

The following example we query the data collection col in:

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

In addition to find () method, there is a findOne () method simply returns a document.


MongoDB compared with RDBMS Where statement

If you are familiar with the general SQL data, the following table may be better understood MongoDB conditional statement query:

operating format example RDBMS Similar statements
equal {<key>:<value> } db.col.find({"by":"本教程"}).pretty() where by = '本教程'
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
more than the {<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 Conditions

MongoDB's find () method can be passed more than one key (key), each key (key) separated by commas, and conventional SQL AND condition.

Syntax is as follows:

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

Examples

The following examples by by title and keys to query the datain this tutorial MongoDB tutorial

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

Similar to the above example, the WHERE clause: WHERE by = 'tutorial' AND title = 'MongoDB Tutorial'


MongoDB OR Conditions

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

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

Examples

The following examples, we demonstrate the value of this tutorial queryby key or key titleisMongoDB tutorialdocumentation.

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

Joint use AND and OR

The following example demonstrates the use of AND and OR joint, similar to the conventional SQL statementis: 'where likes> 50 AND ( by =' 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.w3big.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}