Home >>MongoDB Tutorial >MongoDB Query Document
You need to use MongoDB's find() method to query data from the MongoDB collection.
Syntax
The basic syntax of find() method is as follows −
>db.COLLECTION_NAME.find()
The find() method shows all the documents in a non-structured way.
Example
Assume we have created a collection named mycol as −
> use sampleDB switched to db sampleDB > db.createCollection("mycol") { "ok" : 1 } >
And inserted 3 documents in it using the insert() method as shown below −
> db.mycol.insert([ { title: "MongoDB Overview", description: "MongoDB is no SQL database", by: "phptpoint", url: "http://www.phptpoint.com", tags: ["mongodb", "database", "NoSQL"], likes: 100 }, { title: "NoSQL Database", description: "NoSQL database doesn't have tables", by: "tutorials point", url: "http://www.phptpoint.com", tags: ["mongodb", "database", "NoSQL"], likes: 20, comments: [ { user:"user1", message: "My first comment", dateCreated: new Date(2013,11,10,2,35), like: 0 } ] } ])
The method below retrieves all the documents in the collection.
> db.mycol.find() { "_id" : ObjectId("5dd4e2cc0821d3b44607534c"), "title" : "MongoDB Overview", "description" : "MongoDB is no SQL database", "by" : "phptpoint", "url" : "http://www.phptpoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5dd4e2cc0821d3b44607534d"), "title" : "NoSQL Database", "description" : "NoSQL database doesn't have tables", "by" : "phptpoint", "url" : "http://www.phptpoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 20, "comments" : [ { "user" : "user1", "message" : "My first comment", "dateCreated" : ISODate("2013-12-09T21:05:00Z"), "like" : 0 } ] } >
You may use the pretty() method to display the results in a formatted way.
Syntax
> db.COLLECTION_NAME.find().pretty()
Example
The following example retrieves and arranges all the documents in an easy-to - read format from the collection called mycol.
> db.mycol.find().pretty() { "_id" : ObjectId("5dd4e2cc0821d3b44607534c"), "title" : "MongoDB Overview", "description" : "MongoDB is no SQL database", "by" : "phptpoint", "url" : "http://www.phptpoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } { "_id" : ObjectId("5dd4e2cc0821d3b44607534d"), "title" : "NoSQL Database", "description" : "NoSQL database doesn't have tables", "by" : "phptpoint", "url" : "http://www.phptpoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 20, "comments" : [ { "user" : "user1", "message" : "My first comment", "dateCreated" : ISODate("2013-12-09T21:05:00Z"), "like" : 0 } ] }
There is a findOne() method, apart from the find() method, which returns only one document.
Syntax
> db.COLLECTIONNAME.findOne()
Example
The document with the title MongoDB Overview is retrieved in the following example.
> db.mycol.findOne({title: "MongoDB Overview"}) { "_id" : ObjectId("5dd6542170fb13eec3963bf0"), "title" : "MongoDB Overview", "description" : "MongoDB is no SQL database", "by" : "phptpoint", "url" : "http://www.phptpoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
You can use the following operations to query the document based on that condition.
Operation | Syntax | Example | RDBMS Equivalent |
---|---|---|---|
Equality | {<key>:{$eg;<value>}} | db.mycol.find({"by":"phptpoint "}).pretty() | where by = 'phptpoint' |
Less Than | {<key>:{$lt:<value>}} | db.mycol.find({"likes":{$lt:50}}).pretty() | where likes < 50 |
Less Than Equals | {<key>:{$lte:<value>}} | db.mycol.find({"likes":{$lte:50}}).pretty() | where likes <= 50 |
Greater Than | {<key>:{$gt:<value>}} | db.mycol.find({"likes":{$gt:50}}).pretty() | where likes > 50 |
Greater Than Equals | {<key>:{$gte:<value>}} | db.mycol.find({"likes":{$gte:50}}).pretty() | where likes >= 50 |
Not Equals | {<key>:{$ne:<value>}} | db.mycol.find({"likes":{$ne:50}}).pretty() | where likes != 50 |
Values in an array | {<key>:{$in:[<value1>, <value2>,……<valueN>]}} | db.mycol.find({"name":{$in:["Raj", "Ram", "Raghu"]}}).pretty() | Where name matches any of the value in :["Raj", "Ram", "Raghu"] |
Values not in an array | {<key>:{$nin:<value>}} | db.mycol.find({"name":{$nin:["Ramu", "Raghav"]}}).pretty() | Where name values is not in the array :["Ramu", "Raghav"] or, doesn’t exist at all |
Syntax
You need to use $and a keyword to query documents based on the AND condition. The basic syntax of AND − follows
>db.mycol.find({ $and: [ {<key1>:<value1>}, { <key2>:<value2>} ] })
Example
All tutorials written by 'phptpoint' and whose title is 'MongoDB Overview' are shown in the following example.
> db.mycol.find({$and:[{"by":"phptpoint"},{"title": "MongoDB Overview"}]}).pretty() { "_id" : ObjectId("5dd4e2cc0821d3b44607534c"), "title" : "MongoDB Overview", "description" : "MongoDB is no SQL database", "by" : "phptpoint", "url" : "http://www.phptpoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 } >
Equivalent to where the clause will be 'where by =' tutorials point 'AND title =' MongoDB Overview " for the above example. In the find clause, you can pass any number of key-value pairs.
Syntax
You need to use $or a keyword to query documents based on an OR condition. The basic OR syntax is as follows –
>db.mycol.find( { $or: [ {key1: value1}, {key2:value2} ] } ).pretty()
Example
All the tutorials written by 'phptpoint' or whose title is 'MongoDB Overview' will be shown in the following example.
> db.mycol.find({$or:[{"by":"phptpoint"},{"title": "MongoDB Overview"}]}).pretty() { "_id": ObjectId(7df78ad8902c), "title": "MongoDB Overview", "description": "MongoDB is no sql database", "by": "phptpoint", "url": "http://www.phptpoint.com", "tags": ["mongodb", "database", "NoSQL"], "likes": "100" } >
Example
The following example will show the documents that have likes greater than 10 and whose title is either 'MongoDB Overview' or by is 'tutorials point'. Equivalent SQL where clause is 'where likes>10 AND (by = 'tutorials point' OR title = 'MongoDB Overview')'
>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "phptpoint"}, {"title": "MongoDB Overview"}]}).pretty() { "_id": ObjectId(7df78ad8902c), "title": "MongoDB Overview", "description": "MongoDB is no sql database", "by": "phptpoint", "url": "http://www.phptpoint.com", "tags": ["mongodb", "database", "NoSQL"], "likes": "100" } >
Syntax
You need to use $not a keyword to query documents based on the NOT condition. The simple syntax of NOT – follows
> db.COLLECTION_NAME.find( { $not: [ {key1: value1}, {key2:value2} ] } )
Example
Assume we have inserted 3 documents in the collection empDetails as shown below −
db.empDetails.insertMany( [ { First_Name: "pari", Last_Name: "maurya", Age: "32", e_mail: "pari_maurya.908@gmail.com", phone: "9603212345" }, { First_Name: "Rishika", Last_Name: "choudhary", Age: "20", e_mail: "Rishika_choudhary.357@gmail.com", phone: "9603212345" }, { First_Name: "Fathima", Last_Name: "Sheik", Age: "25", e_mail: "Fathima_Sheik.123@gmail.com", phone: "9603212345" } ] )
The document(s) whose first name is not "Rishika" and last name is not "choudhary" will be retrieved following an example.
> db.empDetails.find( { $nor:[ 40 {"First_Name": " Rishika "}, {"Last_Name": " choudhary "} ] } ).pretty() { "_id" : ObjectId("5dd631f270fb13eec3963bef"), "First_Name" : "Fathima", "Last_Name" : "Sheik", "Age" : "25", "e_mail" : "Fathima_Sheik.123@gmail.com", "phone" : "9603212345" }
Syntax
You must use $not the following keyword to query documents based on the NOT condition, which is the basic syntax of NOT –
> db.COLLECTION_NAME.find( { $NOT: [ {key1: value1}, {key2:value2} ] } ).pretty()
Example
Following example will retrieve the document(s) whose age is not greater than 26
> db.empDetails.find( { "Age": { $not: { $gt: "26" } } } ) { "_id" : ObjectId("5dd6636870fb13eec3963bf7"), "First_Name" : "Fathima", "Last_Name" : "Sheik", "Age" : "25", "e_mail" : "Fathima_Sheik.123@gmail.com", "phone" : "9603212345" }