MongoDB Tutorial Lesson Seventh MongoDB Query Document

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

Find () method

To query the collection data from MongoDB, you need to use the MongoDB find () method.

Grammar

The basic find () method syntax is as follows

>db.  Collection_name.  Find()     

The Find () method displays all the files in an unstructured manner.

Pretty () method

The results are displayed in a formatted manner and can be used with the pretty () method.

Grammar:
>db.  MyCol.  Find().  Pretty()       
Example
>Db.MyCol.Find().Pretty(){ "_ID": ObjectId(7df78ad8902c), "Title": "MongoDB Overview", "description": "MongoDB is no SQL database",  "by" :  " Tutorials Point ", " url ":   "http://www.yiibai.com" , "tags" : [ "MongoDB" Span class= "pun" >,  "database" , "NoSQL" ],  "likes" :< Span class= "PLN" >  "
                                                            
                                                             }>   
                                                                         

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

RDBMS WHERE clause and MongoDB equivalent statement

To query the file based on some conditions, you can use the following actions

Operation Grammar Example RDBMS equivalent
Equality {<key>:<value>} Db.mycol.find ({"By": "Tutorials Point"}). Pretty () where by = ' tutorials point '
Less Than {<key>:{$lt: <value>}} Db.mycol.find ({"likes": {$lt:). Pretty () Where likes < 50
Less Than Equals {<key>:{$lte: <value>}} Db.mycol.find ({"likes": {$lte:). Pretty () where likes <= 50
Greater Than {<key>:{$gt: <value>}} Db.mycol.find ({"likes": {$gt:). Pretty () where likes > 50
Greater Than Equals {<key>:{$gte: <value>}} Db.mycol.find ({"likes": {$gte:). Pretty () where likes >= 50
Not Equals {<key>:{$ne: <value>}} Db.mycol.find ({"likes": {$ne:). Pretty () where likes! = 50
And in MongoDB usage syntax:

In the Find () method, if you detach ', ' through multiple keys, then MongoDB handles the and condition. The and basic syntax is as follows:

>db.  MyCol.  Find({key1:value1, key2:value2}).  Pretty()               
Example

The examples given below will show all tutorials, titled "MongoDB Overview"

>Db.MyCol.Find({"By":"Tutorials Point","Title": "MongoDB Overview"}).Pretty(){ "_ID": ObjectId(7df78ad8902c), "Title": "MongoDB Overview", "description": "MongoDB is no SQL database",  "by" :  " Yiibai ", " url ":  "http://www.yiibai.com" ,  "tags" : [ "MongoDB" ,  "database" ,  "NoSQL"    "likes" :  "
                                                                         
                                                                          }>    
                                                                            

For the example given above is equivalent to the WHERE clause ' where by= ' Yiibai ' and title= ' MongoDB Overview ', can pass any number of key-value pairs in the Find clause.

MongoDB in or syntax:

To query a file based on the or condition, you need to use the $or keyword. The OR basic syntax is as follows:

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

The examples given below will show all tutorials, written by ' Yiibai ' or titled "MongoDB Overview"

>Db.MyCol.Find({$or:[{"By":"Yiibai"},{"Title": "MongoDB Overview"}]}).Pretty(){ "_ID": ObjectId(7df78ad8902c), "Title": "MongoDB Overview", "description": "MongoDB is no SQL database",  "by" :  " Yiibai ", " url ":  "http://www.yiibai.com" ,  "tags" : [ "MongoDB" ,  "database" ,  "NoSQL"    "likes" :  "
                                                                           
                                                                            }>    
                                                                              
Use examples with and and OR

The example given below will show a file with the image greater than 100, with the title "MongoDB Overview" or "Yiibai". Equivalent to the SQL WHERE clause for ' where likes>10 and (by = ' Yiibai ' OR title = ' MongoDB Overview ') '

>Db.MyCol.Find("Likes": {$gt:10},$or: [{"By": "Yiibai"}, {"Title": "MongoDB Overview"}] }).Pretty(){ "_ID": ObjectId(7df78ad8902c), "Title": "MongoDB Overview", "description": "MongoDB is no SQL database",  "by" :  " Yiibai ", " url ":  "http://www.yiibai.com" ,  "tags" : [ "MongoDB" ,  "database" ,  "NoSQL"    "likes" :  "
                                                                                          
                                                                                           }>    
                                                                                             

MongoDB Tutorial Seventh lesson MongoDB querying 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.