MongoDB Query Documents
MongoDB query documents use the find()
method.
The find()
method displays all documents in an unstructured way.
Syntax
The syntax for querying data in MongoDB is as follows:
db.collection.find(query, projection)
- query: Optional, specifies the query criteria using query operators.
- projection: Optional, specifies the fields to return using projection operators. If you want to return all fields in the document, you can omit this parameter (default is omitted).
If you need to read data in a human-readable format, you can use the pretty()
method, which has the following syntax:
> db.col.find().pretty()
The pretty()
method displays all documents in a formatted way.
Example
The following example queries data from the collection col
:
> db.col.find().pretty()
{
"_id" : ObjectId("56063f17ade2f21f36b03133"),
"title" : "MongoDB Tutorial",
"description" : "MongoDB is a Nosql database",
"by" : "tutorialpro.org",
"url" : "http://www.tutorialpro.org",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
In addition to the find()
method, there is also a findOne()
method that returns only one document.
MongoDB vs RDBMS Where Clause
If you are familiar with regular SQL data, the following table helps you better understand MongoDB's conditional statement queries:
Operation | Format | Example | Similar SQL Statement |
---|---|---|---|
Equals | {<key>:<value>} | db.col.find({"by":"tutorialpro.org"}).pretty() | where by = 'tutorialpro.org' |
Less Than | {<key>:{$lt:<value>}} | db.col.find({"likes":{$lt:50}}).pretty() | where likes < 50 |
Less Than or Equal | {<key>:{$lte:<value>}} | db.col.find({"likes":{$lte:50}}).pretty() | where likes <= 50 |
Greater Than | {<key>:{$gt:<value>}} | db.col.find({"likes":{$gt:50}}).pretty() | where likes > 50 |
Greater Than or Equal | {<key>:{$gte:<value>}} | db.col.find({"likes":{$gte:50}}).pretty() | where likes >= 50 |
Not Equal | {<key>:{$ne:<value>}} | db.col.find({"likes":{$ne:50}}).pretty() | where likes != 50 |
MongoDB AND Condition
The find()
method in MongoDB can accept multiple keys, separated by commas, which is similar to the AND condition in regular SQL.
The syntax is as follows:
> db.col.find({key1:value1, key2:value2}).pretty()
Example
The following example queries data from tutorialpro.org
for the "MongoDB Tutorial" using the by and title keys:
> db.col.find({"by":"tutorialpro.org", "title":"MongoDB Tutorial"}).pretty()
{
"_id" : ObjectId("56063f17ade2f21f36b03133"),
"title" : "MongoDB Tutorial",
"description" : "MongoDB is a Nosql database",
"by" : "tutorialpro.org",
"url" : "http://www.tutorialpro.org",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
{
"tags": [
"mongodb",
"database",
"NoSQL"
],
"likes": 100
}
The above example is similar to the WHERE clause: WHERE by='tutorialpro.org' AND title='MongoDB Tutorial'
MongoDB OR Condition
The MongoDB OR condition statement uses the keyword $or, with the following syntax:
>db.col.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
In the following example, we demonstrate querying documents where the key by has the value tutorialpro.org or the key title has the value MongoDB Tutorial.
>db.col.find({$or:[{"by":"tutorialpro.org"},{"title": "MongoDB Tutorial"}]}).pretty()
{
"_id": ObjectId("56063f17ade2f21f36b03133"),
"title": "MongoDB Tutorial",
"description": "MongoDB is a Nosql database",
"by": "tutorialpro.org",
"url": "http://www.tutorialpro.org",
"tags": [
"mongodb",
"database",
"NoSQL"
],
"likes": 100
}
>
Combining AND and OR
The following example demonstrates the combination of AND and OR, similar to the conventional SQL statement: 'where likes>50 AND (by = 'tutorialpro.org' OR title = 'MongoDB Tutorial')'
>db.col.find({"likes": {$gt:50}, $or: [{"by": "tutorialpro.org"},{"title": "MongoDB Tutorial"}]}).pretty()
{
"_id": ObjectId("56063f17ade2f21f36b03133"),
"title": "MongoDB Tutorial",
"description": "MongoDB is a Nosql database",
"by": "tutorialpro.org",
"url": "http://www.tutorialpro.org",
"tags": [
"mongodb",
"database",
"NoSQL"
],
"likes": 100
}