Basic MongoDB Queries

Foridul Islam
5 min readAug 8, 2022

--

A complete guide to understanding the most frequently used MongoDB queries

MongoDB is one of the leading NoSQL databases. Each collection contains a number of JSON documents. Any data model that can be expressed in a JSON document can be easily stored in MongoDB.

MongoDB Query

MongoDB Query is a way to get the data from the MongoDB database. It is similar to SQL queries in the SQL Database language. In MongoDB query, you can use conditions that will be used to retrieve data from the database.

MongoDB Query Syntax

The basic syntax of a MongoDB query looks as follows.

db.COLLECTION_NAME.find({});

Here,

COLLECTION_NAME: is the name of the collection or table in the database

MongoDB query examples

Before executing the MongoDB query, we have to insert some data. We can use insert or insertMany to insert data into a collection of databases.

Consider the following data in a collection name Persons.

db.Persons.insert({ _id : "1", Name: "John", Age: 23, Income : 1000});
db.Persons.insert({ _id : "2", Name: "Don", Age: 27, Income : 3000});
db.Persons.insert({ _id : "3", Name: "Kumar", Age: 21, Income : 4500});
db.Persons.insert({ _id : "4", Name: "Roky", Age: 18, Income : 5000});
db.Persons.insert({ _id : "5", Name: "Smith", Age: 19, Income : 2000});
db.Persons.insert({ _id : "6", Name: "Anik", Age: 35, Income : 1500});
db.Persons.insert({ _id : "7", Name: "Rabi", Age: 32, Income : 2500});
db.Persons.insert({ _id : "8", Name: "Akash", Age: 22, Income : 7000});

Find()

This method is used to display all the documents present in the specific collection. If we run the following MongoDB query in the mongo shell,

db.Persons.find({});

The results of the query are as follows

{ "_id" : "1", "Name" : "John", "Age" : 23, "Income" : 1000 }
{ "_id" : "2", "Name" : "Don", "Age" : 27, "Income" : 3000 }
{ "_id" : "3", "Name" : "Kumar", "Age" : 21, "Income" : 4500 }
{ "_id" : "4", "Name" : "Roky", "Age" : 18, "Income" : 5000 }
{ "_id" : "5", "Name" : "Smith", "Age" : 19, "Income" : 2000 }
{ "_id" : "6", "Name" : "Anik", "Age" : 35, "Income" : 1500 }
{ "_id" : "7", "Name" : "Rabi", "Age" : 32, "Income" : 2500 }
{ "_id" : "8", "Name" : "Akash", "Age" : 22, "Income" : 7000 }

If we want to get the result with the name John, then the query should be as follows.

db.Persons.find({Name: "John"});

The results of the query are as follows

{ "_id" : "1", "Name" : "John", "Age" : 23, "Income" : 1000 }

We can also use FindOne() which is very similar to Find(). But in this case, it retrieves only one document actually the first document that matches the conditions.

The query looks as follows

db.Persons.findOne({});

The results of the query are the first document of the collection.

{ "_id" : "1", "Name" : "John", "Age" : 23, "Income" : 1000 }

MongoDB query using boolean conditions

We can use $and, $or, $in and $not in MongoDB query to retrieve data from the database.

AND Queries

This is very similar to the SQL query with the where clause like,

SELECT * FROM Persons WHERE Name = "John" AND Age >= 20;

The basic syntax of AND query is as follows

db.COLLECTION_NAME.find( {
$and: [
{ key: value }, { key: value }
]
})

If we want to get the documents where the name is John and age is greater than 20. The query is as follows,

db.Persons.find( {
"Name": "John",
"Age": { "$gte": 20 }
})

The results of the query are as follows

OR Queries

This is very similar to the SQL query with the where clause like,

SELECT * FROM Persons WHERE Name = "Smith" OR Age > 23;

The basic syntax of OR query is as follows

db.COLLECTION_NAME.find( {
$or: [
{ key: value }, { key: value }
]
})

If we want to get the documents where the name is Smith or age is greater than 23. The query is as follows,

db.Persons.find( {
"$or": [
{
"Name": "Smith",
},
{
"Age": { "$gt": 23 }
}
]
})

The results of the query are as follows

AND OR Queries

This is very similar to the SQL query with the where clause like,

SELECT * FROM Persons WHERE Name = "Rabi" OR Age > 23 AND Income >= 2000;

If we want to get the documents where the name is Rabi or age is greater than 23 and Income is greater than equal to 2000. The query is as follows,

db.Persons.find( {
"Income": { "$gt": 2000 },
"$or": [
{
"Name": "Rabi"
},
{
"Age":{ "$gt": 23 }
}
]
})

The results of the query are as follows

IN Queries

The $in query is used where we have to use multiple OR queries. It is very similar to the SQL query with the where clause like,

SELECT * FROM Persons WHERE Name IN ("Smith","Kumar");

If we want to get the documents where the name is Smith or Kumar. The query is as follows,

db.Persons.find( {
Name: {"$in": ["Smith","Kumar"]}
})

The results of the query are as follows

NOT Queries

The $not operator is used to perform logical NOT operation on the specified operator expressions and select or retrieve only those documents that do not match the given operator expression.

If we want to get the documents where the name is not started with ‘A’. The query is as follows,

db.Persons.find( { Name: {"$not": /^A.*/} })

The results of the query are as follows

Summary

These are the basic MongoDB queries we discussed. There are also some other query options like Projection, Limit, Skip, Sort, etc are also used with Find(). There also have some other advanced query options that will be discussed later.

Thanks for reading!

--

--

Foridul Islam
Foridul Islam

Written by Foridul Islam

Sr. Software Engineer at SELISE Digital Platforms

No responses yet