MongoDB - Document Query

find() method
To query data from a MongoDB collection, you need to use the find() method of MongoDB.
Syntax
The basic syntax of the find() method is as follows:
>db.COLLECTION_NAME.find()
The find() method will display all documents in an unstructured way.
Pretty() method
To display the results in a formatted way, you can use the pretty() method .
Syntax
>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.tutorialspoint.com" , "tags" : [ "mongodb" , "database" , "NoSQL" ], "
Besides the find() method, there is a findOne() method that returns only one document.
RDBMS where clause equivalents in MongoDB
To request a document based on some condition, you can use the following operations.
operation | Syntax | example | RDBMS equivalent |
---|---|---|---|
equality | {<Key>: <value>} | db.mycol.find({"by": "tutorials point"}). pretty() | where by = 'tutorials point' |
Less than | {<Key>: {$l:<value>}} | db.mycol.find({"likes":{$l:50}}). enough () | where loves <50 |
Less than equal | {<Key>: {$G:<value>}} | db.mycol.find({"likes":{$G:50}}). enough () | where loves <= 50 |
Better than | {<Key>: {$gt:<value>}} | db.mycol.find({"likes":{$Gt:50}}). enough () | where you like > 50 |
Greater Than Equal | {<Key>: {$GTE: <value>}} | db.mycol.find({"likes":{$GTE:50}}). enough () | where likes >= 50 |
Not equal | {<Key>: {$n:<value>}} | db.mycol.find({ "likes": {$n:50}}). enough () | where he loves = 50 |
And in MongoDB
Syntax
In the find() method , if you pass multiple keys separated by ',', MongoDB treats it as an AND condition . Following is the basic syntax of AND −
>db.mycol.find( { $and:[ {key1:value1}, {key2:value2} ] } ).pretty()
example
The following example will show all the tutorials written by "tutorials point" and named "MongoDB Overview".
> db . mycol . find ({ $and :[{ "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" "mongodb" , "database" , "NoSQL" ], "likes" : "100" }
For the above example, the equivalent where clause would 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 documents based on an OR condition, you need to use the $ or keyword. Following is the basic OR syntax −
> db . mycol . find ( { $or : [ { key1 : value1 }, { key2 : value2 } ] } ). pretty ()
example
The following example will show all tutorials written by "point of learning" or named "Overview of MongoDB".
> 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" "mongodb" , "database" , "NoSQL" ], "likes" : "100" } >
Using AND and OR Together
example
The following example will show documents that have a like count greater than 10 and the title is "MongoDB Overview" or "Tutorial". Equivalent SQL where clause 'where likes' > 10 AND (by = 'tutorials point' OR title = 'MongoDB Overview')'