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! = 50AND 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
You should read it
- MongoDB queries and tasks you should know
- Index (Mong) in MongoDB
- Instructions on 2 ways to install MongoDB on Raspberry Pi
- Limit records in MongoDB
- Advantages of MongoDB
- Shard in MongoDB
- Data type in MongoDB
- Delete Document in MongoDB
- MongoDB malicious code attacks more than 26,000 victims in a week
- Learn about security features and authentication in MongoDB
- ObjectId in MongoDB
- Aggregation in MongoDB