MongoDB – Query Document

MongoDB – Query Document

In this chapter, we will learn how to query document from MongoDB collection.

The find() Method

To query data from MongoDB collection, you need to use MongoDB's find() method.

Syntax

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

>db.COLLECTION_NAME.find()

find() method will display all the documents in a non-structured way.

Example

Assume we have created a collection named mydb as −

> use sampleDB
switched to db sampleDB
> db.createCollection("mydb")
{ "ok" : 1 }
>

And inserted 3 documents in it using the insert() method as shown below −

> db.createCollection("mydb")
{ "ok" : 1 }
> db.mydb.insert([{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "rating" : 3.8,"warranty_years" : 1, "available" : true },{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "rating" : 4,"warranty_years" : 1, "available" : false },{ "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "rating" : 4,"warranty_years" : 1, "available" : false }])

Following method retrieves all the documents in the collection −

> db.mydb.find()
{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 200, "rating" : 3.8, "warranty_years" : 1, "available" : true }
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "rating" : 4, "warranty_years" : 1, "available" : false }
{ "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "rating" : 4, "warranty_years" : 1, "available" : false }
> ISODate("2013-12-09T21:05:00Z"), "like" : 0 } ] }
>

The pretty() Method

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

Syntax

>db.mydb.find().pretty()

Example

Following example retrieves all the documents from the collection named mydb and arranges them in an easy-to-read format.

> db.mydb.find().pretty()
{
"_id" : "ac3",
"name" : "AC3 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 200,
"rating" : 3.8,
"warranty_years" : 1,
"available" : true
}
{
"_id" : "ac7",
"name" : "AC7 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 320,
"rating" : 4,
"warranty_years" : 1,
"available" : false
}
{
"_id" : "ac9",
"name" : "AC9 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 320,
"rating" : 4,
"warranty_years" : 1,
"available" : false
}
>

The findOne() method

Apart from the find() method, there is findOne() method, that returns only one document.

Syntax

>db.COLLECTIONNAME.findOne()

Example

Following example retrieves the document with id is ac3 Overview.

> db.mydb.findOne({"_id":"ac3"})
{
"_id" : "ac3",
"name" : "AC3 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 200,
"rating" : 3.8,
"warranty_years" : 1,
"available" : true
}
>

RDBMS Where Clause Equivalents in MongoDB

To query the document on the basis of some condition, you can use following operations.

Operation Syntax Example RDBMS Equivalent
Equality {:{$eg;}} db.mydb.find({"available":true}).pretty() where available = 'true'
Less Than {:{$lt:}} db.mydb.find({"price":{$lt:300}}).pretty() where price < 300
Less Than Equals {:{$lte:}} db.mydb.find({"price":{$lte:320}}).pretty() where price <= 320
Greater Than {:{$gt:}} db.mydb.find({"price":{$gt:200}}).pretty() where price > 300
Greater Than Equals {:{$gte:}} db.mydb.find({"price":{$gte:200}}).pretty() where price >= 200
Not Equals {:{$ne:}} db.mydb.find({"price":{$ne:200}}).pretty() where price != 200
Values in an array {:{$in:[, ,……]}} db.mydb.find({"name":{$in:["AC7 Phone","AC9 Phone","AC11 Phone"]}}).pretty() Where name matches any of the value in :["AC7 Phone","AC9 Phone","AC11 Phone"]
Values not in an array {:{$nin:}} db.mydb.find({"name":{$nin:["AC7 Phone","AC9 Phone","AC11 Phone"]}}).pretty() Where name values is not in the array :["AC7 Phone","AC9 Phone","AC11 Phone"] or, doesn’t exist at all

AND in MongoDB

Syntax

To query documents based on the AND condition, you need to use $and keyword. Following is the basic syntax of AND −

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

Example

Following example will show all the details of phones whose brand is "ACME" and rating is 4.

> db.mydb.find({$and:[{"brand":"ACME"},{"rating":4}]}).pretty();
{
"_id" : "ac7",
"name" : "AC7 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 320,
"rating" : 4,
"warranty_years" : 1,
"available" : false
}
{
"_id" : "ac9",
"name" : "AC9 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 320,
"rating" : 4,
"warranty_years" : 1,
"available" : false
}
>

For the above given example, equivalent where clause will be ' where "brand"="ACME" AND "rating" = 4. You can pass any number of key, value pairs in find clause.

OR in MongoDB

Syntax

To query documents based on the OR condition, you need to use $or keyword. Following is the basic syntax of OR −

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

Example

Following example will show all the details of phones whose "_id" is ac9 "ACME" or price is 200.

> db.mydb.find({$or:[{"_id":"ac9"},{"price":200}]}).pretty();
{
"_id" : "ac3",
"name" : "AC3 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 200,
"rating" : 3.8,
"warranty_years" : 1,
"available" : true
}
{
"_id" : "ac9",
"name" : "AC9 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 320,
"rating" : 4,
"warranty_years" : 1,
"available" : false
}
>

Using AND and OR Together

Example

The following example will show the documents that have price greater than 10 and whose id is either 'ac9' or price is 320. Equivalent SQL where clause is 'where price>200 AND ("_id" = 'ac9' OR price = 320)'

> db.mydb.find({"price":{$gt:200},$or:[{"_id":"ac9"},{"price":320}]})
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "rating" : 4, "warranty_years" : 1, "available" : false }
{ "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "rating" : 4, "warranty_years" : 1, "available" : false }
>

NOR in MongoDB

Syntax

To query documents based on the NOT condition, you need to use $not keyword. Following is the basic syntax of NOT −

>db.COLLECTION_NAME.find(
{
$not: [
{key1: value1}, {key2:value2}
]
}
)

Example

Assume we have inserted 3 documents in the collection mydb as shown below −

> db.mydb.insertMany([
{ "_id" : "ac11",
"name" : "AC11 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 340,
"rating" : 4,
"warranty_years" : 1,
"available" : false
},
{
"_id" : "ac12",
"name" : "AC12 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 450,
"rating" : 4.6,
"warranty_years" : 2,
"available" : true },
{
"_id" : "ac13",
"name" : "AC13 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 6000,
"rating" : 5,
"warranty_years" : 2.5,
"available" : true
}])
{ "acknowledged" : true, "insertedIds" : [ "ac11", "ac12", "ac13" ] }
>

Following example will retrieve the document(s) whose price is not 320 and rating not 4.

> db.mydb.find({$nor:[{"price":320},{"rating":4}]}).pretty()
{
"_id" : "ac3",
"name" : "AC3 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 200,
"rating" : 3.8,
"warranty_years" : 1,
"available" : true
}
{
"_id" : "ac12",
"name" : "AC12 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 450,
"rating" : 4.6,
"warranty_years" : 2,
"available" : true
}
{
"_id" : "ac13",
"name" : "AC13 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 6000,
}

NOT in MongoDB

Syntax

To query documents based on the NOT condition, you need to use $not keyword following 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 price is not greter than 400

> db.mydb.find({"price":{$not:{$gt:400}}}).pretty()
{
"_id" : "ac3",
"name" : "AC3 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 200,
"rating" : 3.8,
"warranty_years" : 1,
"available" : true
}
{
"_id" : "ac7",
"name" : "AC7 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 320,
"rating" : 4,
"warranty_years" : 1,
"available" : false
}
{
"_id" : "ac9",
"name" : "AC9 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 320,
"rating" : 4,
"warranty_years" : 1,
"available" : false
}
{
"_id" : "ac11",
"name" : "AC11 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 340,
"rating" : 4,
"warranty_years" : 1,
"available" : false
}
>
MongoDB – Insert Document (Prev Lesson)
(Next Lesson) MongoDB – Update Document