MongoDB Query Document

Source: Internet
Author: User
Tags mongodb find mongodb query mongodb tutorial

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, which has the following syntax:

>db.  Col.  Find().  Pretty()       

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

Instance

The following examples let us query the data in the set col:

>Db.Ao..Find().Pretty(){ "_ID" : ObjectId("56063f17ade2f21f36b03133"), "Title" : "MongoDB Tutorial", "description" : "MongoDB is a Nosql database", "By" :  "rookie Tutorial" ,  "url"  :  "http://www.runoob.com" ,  "tags"  :[  "MongoDB" ,  "database" ,  "NoSQL"  ],  "likes"  :  100}        

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

MongoDB versus RDBMS Where statement comparison

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

Operation format Example similar statements in an RDBMS
Equals {<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
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

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

The syntax format is as follows:

>db.  Col.  Find({key1:value1, key2:value2}).  Pretty()               
Instance

The following example uses the by and title keys to query the data for the MongoDB tutorial in the rookie tutorial

>Db.Col.Find({"By":"Beginner's Tutorial", "Title":"MongoDB Tutorial"}).Pretty(){ "_ID" : ObjectId("56063f17ade2f21f36b03133"), "Title" : "MongoDB Tutorial", "description" : "MongoDB is a Nosql database", "By" :  "rookie Tutorial" ,  "url"  :  "http://www.runoob.com" ,  "tags"  :[  "MongoDB" ,  "database" ,  "NoSQL"  ],  "likes"  :  100}        

The above example is similar to the WHERE statement:where by= ' rookie tutorial ' and title= ' MongoDB tutorial '

MongoDB OR Condition

The MongoDB OR conditional statement uses the keyword $orin the following syntax format:

>db.  Col.  Find({ $or:[{key1: value1},{key2:value2} ]}). Pretty()                 
Instance

In the following example, we demonstrate the query key by value for the Novice tutorial or the key title value for the MongoDB tutorial document.

>Db.Ao..Find({$or:[{"By":"Beginner's Tutorial"},{"Title": "MongoDB Tutorial"}]}).Pretty(){ "_ID" : ObjectId("56063f17ade2f21f36b03133"), "Title" : "MongoDB Tutorial", "description" : "MongoDB is a Nosql database", "By" : "Rookie Tutorial", "url" : "http://www.runoob.com", "tags" : [  "MongoDB", "database", "NoSQL" ], "likes" :  >               
Combined with and OR

The following examples demonstrate the combined use of and and or, similar to the general SQL statement: ' where likes>50 and (by = ' Rookie Tutorial ' OR title = ' MongoDB tutorial ') '

>Db.Col.Find({"Likes": {$gt:50},$or: [{"By": "Beginner's Tutorial"},{"Title": "MongoDB Tutorial"}]}).Pretty(){ "_ID" : ObjectId("56063f17ade2f21f36b03133"), "Title" : "MongoDB Tutorial", "description" : "MongoDB is a Nosql database", "By" :  "rookie Tutorial" ,  "url"  :  "http://www.runoob.com" ,  "tags"  :[  "MongoDB" ,  "database" ,  "NoSQL"  ],  "likes"  :  100}        

MongoDB Query Document

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.