Hurry! 20% Off Ends SoonRegister Now

MongoDB Query With Examples

This article provides a list of all available queries that can be used on the MongoDB database server (version v3.4.7) and the entirety of its working depends on the version of the MongoDB instance that you are using.

For example, the Map/Reduce feature was available on the MongoDB database server until version v2.2 and it no longer exists in version v3.4.7 and this has been replaced with the Aggregation feature. Hence it is requested to be very specific with the version of MongoDB that is being used.

Let us take a look at the possible commonly used queries on MongoDB which will help in your day to day activities with the database itself. This MongoDB Query With Examples article covers most of the areas of MongoDB database administration, and any further read (if required), then the official MongoDB reference can be reviewed.

If you want to enrich your career and become a professional in MongoDB, then visit Mindmajix - a global online training platform: "MongoDB Online Training" This course will help you to achieve excellence in this domain. 

MongoDB Queries Listing with Examples

The table below lists the most commonly used queries on a given MongoDB database instance version (v3.4.7) and description, a syntax for its ready usage.

Let us consider a sample collection structure so that the operations can be applied to this data set (as provided below) and the resultant result set can be showcased by each of the operations from the tabular columns.

db.createCollection(“SampleDataCollection”)
db.SampleDataCollection.insert([
{  _id : 1, episodeName : “Dragonstone”, directedBy : “Jeremy Podeswa”, writtenBy : “David Benioff, D.B.Weiss”, airedDate : “July2017”},
{  _id : 2, episodeName: “Stormborn”, directedBy : “Mark Mylod”, writtenBy : “Bryan Cogman”, airedDate : “July2017”},
{ _id : 3, episodeName: “The Queen’s Justice”, directedBy : “Mark Mylod”, writtenBy : “David Benioff, D.B.Weiss”, airedDate : “July2017”},
{  _id : 4, episodeName: “The Spoils of War”, directedBy : “Matt Shakman”, writtenBy : “David Benioff, D.B.Weiss”, airedDate: “August2017”},
{  _id : 5, episodeName: “Eastwatch”, directedBy : “Matt Shakman”, writtenBy : “Bryan Cogman”, airedDate: ”August2017”}
])

MindMajix Youtube Channel

Operations 

Example 1

db.collection.find()

Description

This is equivalent to a READ query with a specified WHERE clause in the form of the condition in the RDBMS world.

Query: db.SampleDataCollection.find()
RDMS Equivalent: SELECT * FROM SampleDataCollection

Result:

The above query will return the complete table data as result, as there is no condition to satisfy on the READ call.

Query: db.SampleDataCollection.find( _id : 5)

RDBMS Equivalent: SELECT * FROM SampleDataCollection WHERE _id = 5

Result:

{

"_id" : 5,

"episodeName" : "Eastwatch",

"directedBy" : "Matt Shakman",

"writtenBy" : "Bryan Cogman",

"airedDate" : "August2017"

}

[Related Article: MongoDB Tutorial]

Example 2

db.collection.find(, )

Description

This is equivalent to a READ query with a specified WHERE and specified columns to be retrieved from the table in the RDBMS world.

Query : db.SampleDataCollection.find( { _id : 5}, { episodeName : 1, writtenBy : 1})
RDBMS Equivalent : SELECT _id, episodeName, writtenBy 
                                      FROM SampleDataCollection 
                                      WHERE _id = 5

Result:

{
"_id" : 5,
"directedBy" : "Matt Shakman",
"writtenBy" : "Bryan Cogman"
}

Example 3

db.collection.find().sort()

Description

This is equivalent to a SELECT query ORDERED BY a certain column

Query: db.SampleDataCollection.find().sort( { _id : -1 })
RDBMS Equivalent: SELECT * FROM SampleDataCollection ORDER BY _id DESC

Result:

/* 1 */
{
"_id" : 5,
"episodeName" : "Eastwatch",
"directedBy" : "Matt Shakman",
"writtenBy" : "Bryan Cogman",
"airedDate" : "August2017"
},
/* 2 */
{
"_id" : 4,
"episodeName" : "The Spoils of War",
"directedBy" : "Matt Shakman",
"writtenBy" : "David Benioff, D.B.Weiss",
"airedDate" : "August2017"
},
/* 3 */
{
"_id" : 3,
"episodeName" : "The Queen’s Justice",
"directedBy" : "Mark Mylod",
"writtenBy" : "David Benioff, D.B.Weiss",
"airedDate" : "July2017"
},
/* 4 */
{
"_id" : 2,
"episodeName" : "Stormborn",
"directedBy" : "Mark Mylod",
"writtenBy" : "Bryan Cogman",
"airedDate" : "July2017"
},
/* 5 */
{
"_id" : 1,
"episodeName" : "Dragonstone",
"directedBy" : "Jeremy Podeswa",
"writtenBy" : "David Benioff, D.B.Weiss",
"airedDate" : "July2017"
}

Exampe 4

db.collection.find().sort()

Description

This is equivalent to a SELECT query with a WHERE clause and then ORDERED BY a column specified in the query.

Query : db.SampleDataCollection.find({ directedBy : “Mark Mylod”}).sort( { _id : -1 })
RDBMS Equivalent: SELECT * FROM SampleDataCollection 
                                    WHERE directedBY = “Mark Mylod” 
                                    ORDER BY _id DESC

Result:

/* 1 */
{
"_id" : 3,
"episodeName" : "The Queen’s Justice",
"directedBy" : "Mark Mylod",
"writtenBy" : "David Benioff, D.B.Weiss",
"airedDate" : "July2017"
},
/* 2 */
{
"_id" : 2,
"episodeName" : "Stormborn",
"directedBy" : "Mark Mylod",
"writtenBy" : "Bryan Cogman",
"airedDate" : "July2017"
}

[Related Article: MongoDB Interview Question and Answers]

Example 5

db.collection.find().limit( )

Description

This is equivalent to a SELECT query with a TOP or LIMIT condition based on the RDBMS database type.

Query : db.SampleDataCollection.find().limit(2)
RDBMS Equivalent : SELECT * FROM SampleDataCollection LIMIT 2

Result:

/* 1 */
{
"_id" : 1,
"episodeName" : "Dragonstone",
"directedBy" : "Jeremy Podeswa",
"writtenBy" : "David Benioff, D.B.Weiss",
"airedDate" : "July2017"
},
/* 2 */
{
"_id" : 2,
"episodeName" : "Stormborn",
"directedBy" : "Mark Mylod",
"writtenBy" : "Bryan Cogman",
"airedDate" : "July2017"
}

Example 6

db.collection.find( ).limit( )

Description

This is equivalent to a SELECT query with a LIMIT condition with a WHERE clause on a specified field of the table.

Query : db.SampleDataCollection.find( {airedDate : “July2017”} ).limit(2)
RDBMS Equivalent : SELECT * FROM SampleDataCollection
                                     WHERE airedDate = ‘July2017’
                                     LIMIT 2

Result:

/* 1 */
{
"_id" : 1,
"episodeName" : "Dragonstone",
"directedBy" : "Jeremy Podeswa",
"writtenBy" : "David Benioff, D.B.Weiss",
"airedDate" : "July2017"
},
/* 2 */
{
"_id" : 2,
"episodeName" : "Stormborn",
"directedBy" : "Mark Mylod",
"writtenBy" : "Bryan Cogman",
"airedDate" : "July2017"
}

Example 7

db.collection.find().skip( )

Description

This is equivalent to a SELECT query with a WHERE condition on the primary key to skip those many rows from the beginning of the table.

Query : db.SampleDataCollection.find().skip(4)
RDBMS Equivalent : SELECT * FROM SampleDataCollection WHERE _id > 4

Result:

{
"_id" : 5,
"episodeName" : "Eastwatch",
"directedBy" : "Matt Shakman",
"writtenBy" : "Bryan Cogman",
"airedDate" : "August2017"
}

Example 8

db.collection.find( ).skip( )

Description

This is equivalent to a SELECT query with a WHERE condition on a specified column and skip n number of rows from the resultant result set.

Query : db.SampleDataCollection.find( { directedBy : “Matt Shakman” } ).skip(1)
RDBMS Equivalent : SELECT * FROM SampleDataCollection 
                                     WHERE directedBy = ‘Matt Shakman’ 

Result : 

{

"_id" : 5,

"episodeName" : "Eastwatch",

"directedBy" : "Matt Shakman",

"writtenBy" : "Bryan Cogman",

"airedDate" : "August2017"

}

Example 9

db.collection.count()

Description

This is equivalent to a SELECT COUNT(*) on a given table

Query : db.SampleDataCollection.count()

RDBMS Equivalent : SELECT COUNT(*) FROM SampleDataCollection

Result : 5

[Related Article: MongoDB Find Queries]

Example 10

db.collection.find( ).count()

Description

This is equivalent to a SELECT COUNT(*) on a given table with a WHERE clause

Query : db.SampleDataCollection.find({ airedDate : “August2017” }).count()

RDBMS Equivalent : SELECT COUNT(*) FROM SampleDataCollection WHERE airedDate = ‘August2017’

Result : 2

Example 11

db.collection.findOne( )

Description

This is equivalent to find and return only one record from the resultant result set.

Query : db.SampleDataCollection.findOne({ airedDate : “August2017” })

RDBMS Equivalent : SELECT TOP 1 * FROM SampleDataCollection WHERE airedDate = ‘August2017’

Result :

{

"_id" : 4,

"episodeName" : "The Spoils of War",

"directedBy" : "Matt Shakman",

"writtenBy" : "David Benioff, D.B.Weiss",

"airedDate" : "August2017"

}

Conclusion:

The intent of the MongoDB Query With Examples article is to provide a list of all the possible commands that can be used on a given MongoDB database instance version (v.3.4.7). As mentioned earlier, the entirety of these MongoDB commands may not work as-is on any other version of MongoDB database instance as there are features dropped and added with each possible version of MongoDB.

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
MongoDB Training Dec 24 to Jan 08View Details
MongoDB Training Dec 28 to Jan 12View Details
MongoDB Training Dec 31 to Jan 15View Details
MongoDB Training Jan 04 to Jan 19View Details
Last updated: 03 Apr 2023
About Author

Prasanthi is an expert writer in MongoDB, and has written for various reputable online and print publications. At present, she is working for MindMajix, and writes content not only on MongoDB, but also on Sharepoint, Uipath, and AWS.

read less