Query Document in MongoDB

The find () method in MongoDB

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

Syntax

The basic syntax of find () method is as follows:

 > db . COLLECTION_NAME . find () 

The find () method will display all Documents in unstructured form (show no structure).

Pretty () method in MongoDB

To display the results in a formatted way, you can use the pretty () method .

Syntax

 > db . mycol . find (). pretty () 

For example

 > db . mycol . find (). pretty () { "_id" : ObjectId ( 7df78ad8902c ), "title" : "MongoDB Overview" , "description" : "MongoDB is no sql database" , "by" : "tutorials point" , "url" : "http://www.tutorialspoint.com" , "tags" : [ "mongodb" , "database" , "NoSQL" ], "likes" : "100" } > 

In addition to the find () method, in MongoDB there's a findOne () method that will return only a Document.

Query in MongoDB which is equivalent to the WHERE clause in RDBMS

To query a Document based on certain conditions, you can use the following operations:

Math MethodSample Example WHERE clause equivalentEquality {:} db.mycol.find ({"by": "tutorials point"}). Pretty () where by = 'tutorials point' Less Than {: {$ lt:}} db .mycol.find ({"likes": {$ lt: 50}}). pretty () where likes 50 Greater Than Equals {: {$ gte:}} db.mycol.find ({"likes": {$ gte: 50}}) pretty () where likes> = 50 Not Equals {: {$ ne:} } db.mycol.find ({"likes": {$ ne: 50}}) pretty. () where likes! = 50

AND in MongoDB

Syntax

In the find () method, if you pass multiple keys by distinguishing them by commas (,), MongoDB sees it as an AND condition. The basic syntax of AND in MongoDB is as follows:

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

For example

The following example shows all tutorial series (tutorials) written by 'tutorials point' which has the title '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" : "tutorials point" , "url" : "http://www.tutorialspoint.com" , "tags" : [ "mongodb" , "database" , "NoSQL" ], "likes" : "100" } > 

The WHERE clause equivalent to the above example will be 'where by =' tutorials point 'AND title =' MongoDB Overview '' . You can pass any number of key-value pairs in the find clause.

OR in MongoDB

Syntax

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

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

For example

The following example will show all the tutorial series (tutorials) written by 'tutorials point' or have the title 'MongoDB Overview'

 > db . mycol . find ({ $or :[{ "by" : "tutorials point" },{ "title" : "MongoDB Overview" }]}). pretty () { "_id" : ObjectId ( 7df78ad8902c ), "title" : "MongoDB Overview" , "description" : "MongoDB is no sql database" , "by" : "tutorials point" , "url" : "http://www.tutorialspoint.com" , "tags" : [ "mongodb" , "database" , "NoSQL" ], "likes" : "100" } > 

Use AND and OR together in MongoDB

For example

The following example shows documents that have likes greater than 100 and whose title is either 'MongoDB Overview' or 'tutorials point'. The WHERE clause in the equivalent SQL query is 'where likes> 10 AND (by =' tutorials point 'OR title =' MongoDB Overview ')'

 > db . mycol . find ({ "likes" : { $gt : 10 }, $or : [{ "by" : "tutorials point" },{ "title" : "MongoDB Overview" }]}). pretty () { "_id" : ObjectId ( 7df78ad8902c ), "title" : "MongoDB Overview" , "description" : "MongoDB is no sql database" , "by" : "tutorials point" , "url" : "http://www.tutorialspoint.com" , "tags" : [ "mongodb" , "database" , "NoSQL" ], "likes" : "100" } > 

According to Tutorialspoint

Previous article: Insert Document in MongoDB

Next article: Update Document in MongoDB

4 ★ | 1 Vote

May be interested

  • Sort records in MongoDBSort records in MongoDB
    to sort documents in mongodb, you need to use the sort () method. the sort () method takes a document containing a list of fields with their sort order. to determine the sort order, 1 and -1 are used. 1 is used for ascending order, while -1 is used for descending order.
  • Instructions on 2 ways to install MongoDB on Raspberry PiInstructions on 2 ways to install MongoDB on Raspberry Pi
    in this tutorial, tipsmake will guide you through the process of installing and setting up the mongodb server software on your raspberry pi.
  • How to save React form data in Mongo DatabaseHow to save React form data in Mongo Database
    try adding mongodb to your web stack to see how easy it is to store and query form data!
  • Reference Database in MongoDBReference Database in MongoDB
    as shown in the relationship chapter in mongodb, to deploy a standardized database structure in mongodb, we use the referenced relationship concept, also known as manual references, in which we manipulate to store the id of the documents referenced in another document.
  • Capped Collection in MongoDBCapped Collection in MongoDB
    capped collections are fixed-sized circular collection that follow the insert order to enhance the performance of create, read, and delete operations.
  • Limit records in MongoDBLimit records in MongoDB
    to limit the records in mongodb, you need to use the limit () method. the limit () method takes a parameter in a numeric format, which is the document number you want to display.
  • The multiple-choice question set has an answer to Query P1The multiple-choice question set has an answer to Query P1
    in the previous articles, the network administrator sent you read query queries. following the series on this topic, below is the next part of the questionnaire, invite readers to try.
  • Advantages of MongoDBAdvantages of MongoDB
    any relation database has a unique schema design to index the data tables and relationships between those tables. meanwhile in mongodb there is no concept of relationship.
  • Data type in MongoDBData type in MongoDB
    mongodb supports many different data types. the following are some typical data types.
  • MongoDB malicious code attacks more than 26,000 victims in a weekMongoDB malicious code attacks more than 26,000 victims in a week
    malware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.