Blog

MongoDB Query With Examples

  • (4.0)
  •   |   666 Ratings

MongoDB Query List

Introduction:

This article provides a list of all available queries that can be used on MongoDB database server (verion v3.4.7) and the entirety of its working depends on the version of the MongoDB instance that you are using. For example, Map/Reduce feature was available on 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 article covers most of the areas of MongoDB database administration, and any further read (if required), then the official MongoDB reference can be reviewed.

Interested in mastering MongoDB Course? Enroll now for FREE demo on MongoDB Training.

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 on 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”}
])

 

Operation Description & Example
db.collection.find()
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"
}
db.collection.find(, )
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"
}
db.collection.find().sort()
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"
}
db.collection.find().sort()
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"
}
db.collection.find().limit( )
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"
}
db.collection.find( ).limit( )
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"
}
db.collection.find().skip( )
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"
}
db.collection.find( ).skip( )
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"
}
db.collection.count()

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

Query : db.SampleDataCollection.count()
RDBMS Equivalent : SELECT COUNT(*) FROM SampleDataCollection
Result : 5

db.collection.find( ).count()

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

db.collection.findOne( )
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 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 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.

Related Pages:


Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules