MongoDB
In this MongoDB tutorial we will learn to query documents.
To select all the documents in a collection we use the find()
method.
In the following example we are listing all the documents in the students collection.
> db.students.find();
{ "_id" : ObjectId("5d16c9e9e8cb73839ac9f2f1"), "firstname" : "Yusuf", "lastname" : "Shakeel", "studentid" : "s01" }
{ "_id" : ObjectId("5d16c9efe8cb73839ac9f2f2"), "firstname" : "Jane", "lastname" : "Doe", "studentid" : "s02" }
{ "_id" : ObjectId("5d16c9efe8cb73839ac9f2f3"), "firstname" : "John", "lastname" : "Doe", "studentid" : "s03" }
{ "_id" : ObjectId("5d16ca23e8cb73839ac9f2f4"), "firstname" : "Alice", "lastname" : "Doe", "studentid" : "s04", "score" : 10.5 }
{ "_id" : ObjectId("5d16ca23e8cb73839ac9f2f5"), "firstname" : "Bob", "lastname" : "Doe", "studentid" : "s05", "date_of_birth" : ISODate("2000-01-01T00:00:00Z") }
{ "_id" : ObjectId("5d16cfa4e8cb73839ac9f2f6"), "firstname" : "Eve", "lastname" : "Doe", "studentid" : "s06", "contact_phone" : { "primary" : { "number" : "+919800000000", "name" : "Bill Doe", "relation" : "Father" }, "secondary" : [ { "number" : "+919800000001", "name" : "Mac Doe", "relation" : "Brother" } ] } }
To render the result in easy to read format we use the pretty()
method.
In the following example we are selecting all the documents in the students collection and listing them in an easy-to-read format.
> db.students.find().pretty();
{
"_id" : ObjectId("5d16c9e9e8cb73839ac9f2f1"),
"firstname" : "Yusuf",
"lastname" : "Shakeel",
"studentid" : "s01"
}
{
"_id" : ObjectId("5d16c9efe8cb73839ac9f2f2"),
"firstname" : "Jane",
"lastname" : "Doe",
"studentid" : "s02"
}
{
"_id" : ObjectId("5d16c9efe8cb73839ac9f2f3"),
"firstname" : "John",
"lastname" : "Doe",
"studentid" : "s03"
}
{
"_id" : ObjectId("5d16ca23e8cb73839ac9f2f4"),
"firstname" : "Alice",
"lastname" : "Doe",
"studentid" : "s04",
"score" : 10.5
}
{
"_id" : ObjectId("5d16ca23e8cb73839ac9f2f5"),
"firstname" : "Bob",
"lastname" : "Doe",
"studentid" : "s05",
"date_of_birth" : ISODate("2000-01-01T00:00:00Z")
}
{
"_id" : ObjectId("5d16cfa4e8cb73839ac9f2f6"),
"firstname" : "Eve",
"lastname" : "Doe",
"studentid" : "s06",
"contact_phone" : {
"primary" : {
"number" : "+919800000000",
"name" : "Bill Doe",
"relation" : "Father"
},
"secondary" : [
{
"number" : "+919800000001",
"name" : "Mac Doe",
"relation" : "Brother"
}
]
}
}
The find()
method is equivalent to the following SQL statement.
SELECT * FROM students;
To check the total number of documents in a collection we use the count()
method.
> db.students.count({})
6
Note! The first parameter of count
method is query. Since we want to count total documents without any filter so, we are passing {}
as the query paramter.
We can also use the countDocuments()
method.
> db.students.countDocuments({})
6
Note! The first parameter of countDocuments
method is query. Since we want to count total documents without any filter so, we are passing {}
as the query paramter.
In the following example we are selecting all the douments having value Doe
for the lastname
field in the students collection.
> db.students.find({ "lastname": "Doe" });
{ "_id" : ObjectId("5d16c9efe8cb73839ac9f2f2"), "firstname" : "Jane", "lastname" : "Doe", "studentid" : "s02" }
{ "_id" : ObjectId("5d16c9efe8cb73839ac9f2f3"), "firstname" : "John", "lastname" : "Doe", "studentid" : "s03" }
{ "_id" : ObjectId("5d16ca23e8cb73839ac9f2f4"), "firstname" : "Alice", "lastname" : "Doe", "studentid" : "s04", "score" : 10.5 }
{ "_id" : ObjectId("5d16ca23e8cb73839ac9f2f5"), "firstname" : "Bob", "lastname" : "Doe", "studentid" : "s05", "date_of_birth" : ISODate("2000-01-01T00:00:00Z") }
{ "_id" : ObjectId("5d16cfa4e8cb73839ac9f2f6"), "firstname" : "Eve", "lastname" : "Doe", "studentid" : "s06", "contact_phone" : { "primary" : { "number" : "+919800000000", "name" : "Bill Doe", "relation" : "Father" }, "secondary" : [ { "number" : "+919800000001", "name" : "Mac Doe", "relation" : "Brother" } ] } }
Similarly, we can use two or more fields to filter the documents.
In the following example we are selecting documents having firstname
equal to John
and lastname
as Doe
.
> db.students.find({ "firstname": "John", "lastname": "Doe" });
{ "_id" : ObjectId("5d16c9efe8cb73839ac9f2f3"), "firstname" : "John", "lastname" : "Doe", "studentid" : "s03" }
We can control the fields that we want to return in our result.
Syntax:
> db.collectionName.find({ /*query*/ }, { /*fields*/ });
To include specific fields we mention the field name and set it to 1.
In the following example we want to return the firstname and lastname of the students.
> db.students.find({}, { "firstname": 1, "lastname": 1 });
{ "_id" : ObjectId("5d16c9e9e8cb73839ac9f2f1"), "firstname" : "Yusuf", "lastname" : "Shakeel" }
{ "_id" : ObjectId("5d16c9efe8cb73839ac9f2f2"), "firstname" : "Jane", "lastname" : "Doe" }
{ "_id" : ObjectId("5d16c9efe8cb73839ac9f2f3"), "firstname" : "John", "lastname" : "Doe" }
{ "_id" : ObjectId("5d16ca23e8cb73839ac9f2f4"), "firstname" : "Alice", "lastname" : "Doe" }
{ "_id" : ObjectId("5d16ca23e8cb73839ac9f2f5"), "firstname" : "Bob", "lastname" : "Doe" }
{ "_id" : ObjectId("5d16cfa4e8cb73839ac9f2f6"), "firstname" : "Eve", "lastname" : "Doe" }
Note! The _id
field is returned by default. To prevent it from appearing in our output we have to set "_id": 0
.
In the following example we are filtering the firstname and lastname of the students without the _id field.
> db.students.find({}, { "firstname": 1, "lastname": 1, "_id": 0 });
{ "firstname" : "Yusuf", "lastname" : "Shakeel" }
{ "firstname" : "Jane", "lastname" : "Doe" }
{ "firstname" : "John", "lastname" : "Doe" }
{ "firstname" : "Alice", "lastname" : "Doe" }
{ "firstname" : "Bob", "lastname" : "Doe" }
{ "firstname" : "Eve", "lastname" : "Doe" }
To exclude specific fields from the result we mention the field name and set it to 0.
In the following query we are returning all the fields but excluding "studentid" field.
> db.students.find({}, { "studentid": 0 });
{ "_id" : ObjectId("5d16c9e9e8cb73839ac9f2f1"), "firstname" : "Yusuf", "lastname" : "Shakeel" }
{ "_id" : ObjectId("5d16c9efe8cb73839ac9f2f2"), "firstname" : "Jane", "lastname" : "Doe" }
{ "_id" : ObjectId("5d16c9efe8cb73839ac9f2f3"), "firstname" : "John", "lastname" : "Doe" }
{ "_id" : ObjectId("5d16ca23e8cb73839ac9f2f4"), "firstname" : "Alice", "lastname" : "Doe", "score" : 10.5 }
{ "_id" : ObjectId("5d16ca23e8cb73839ac9f2f5"), "firstname" : "Bob", "lastname" : "Doe", "date_of_birth" : ISODate("2000-01-01T00:00:00Z") }
{ "_id" : ObjectId("5d16cfa4e8cb73839ac9f2f6"), "firstname" : "Eve", "lastname" : "Doe", "contact_phone" : { "primary" : { "number" : "+919800000000", "name" : "Bill Doe", "relation" : "Father" }, "secondary" : [ { "number" : "+919800000001", "name" : "Mac Doe", "relation" : "Brother" } ] } }
We can use the following logical operators to filter documents.
Operator | Operation | Example |
---|---|---|
$lt | Less than | { field: { $lt: value } } |
$lte | Less than or equal to | { field: { $lte: value } } |
$gt | Greater than | { field: { $gt: value } } |
$gte | Greater than or equal to | { field: { $gte: value } } |
$ne | Not equal | { field: { $ne: value } } |
$eq | Equal to | { field: { $eq: value } } |
For logical operator example we will use the scoreboard collection.
> db.scoreboard.find();
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2f9"), "team" : "Apple", "score" : 20 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fa"), "team" : "Banana", "score" : 15 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fb"), "team" : "Pineapple", "score" : 10 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fc"), "team" : "Watermelon", "score" : 30 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fd"), "team" : "Mango", "score" : 5 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fe"), "team" : "Orange", "score" : 25 }
For this we use the $lt
less than operator.
> db.scoreboard.find({
"score": { $lt: 15 }
});
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fb"), "team" : "Pineapple", "score" : 10 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fd"), "team" : "Mango", "score" : 5 }
For this we use the $lte
less than or equal to operator.
> db.scoreboard.find({
"score": { $lte: 15 }
});
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fa"), "team" : "Banana", "score" : 15 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fb"), "team" : "Pineapple", "score" : 10 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fd"), "team" : "Mango", "score" : 5 }
For this we use the $gt
greater than operator.
> db.scoreboard.find({
"score": { $gt: 20 }
});
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fc"), "team" : "Watermelon", "score" : 30 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fe"), "team" : "Orange", "score" : 25 }
For this we use the $gte
greater than or equal to operator.
> db.scoreboard.find({
"score": { $gte: 20 }
});
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2f9"), "team" : "Apple", "score" : 20 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fc"), "team" : "Watermelon", "score" : 30 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fe"), "team" : "Orange", "score" : 25 }
For this we use the $ne
not equal to operator.
> db.scoreboard.find({
"score": { $ne: 5 }
});
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2f9"), "team" : "Apple", "score" : 20 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fa"), "team" : "Banana", "score" : 15 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fb"), "team" : "Pineapple", "score" : 10 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fc"), "team" : "Watermelon", "score" : 30 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fe"), "team" : "Orange", "score" : 25 }
For this we use the $eq
equal to operator.
> db.scoreboard.find({
"score": { $eq: 5 }
});
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fd"), "team" : "Mango", "score" : 5 }
To filter documents based on a range of values we can either use in ($in) and not in ($nin) operators.
The $in
operators will check if the value of a field is matching any of the given values.
Format: { field: { $in: [value1, value2, ... ] } }
The $nin
operators will check if the value of a field is not matching any of the given values.
Format: { field: { $nin: [value1, value2, ... ] } }
For this we use the $in
in operator.
> db.scoreboard.find({
"score": { $in: [5, 15, 20] }
});
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2f9"), "team" : "Apple", "score" : 20 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fa"), "team" : "Banana", "score" : 15 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fd"), "team" : "Mango", "score" : 5 }
For this we use the $nin
not in operator.
> db.scoreboard.find({
"score": { $nin: [5, 15, 20] }
});
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fb"), "team" : "Pineapple", "score" : 10 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fc"), "team" : "Watermelon", "score" : 30 }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fe"), "team" : "Orange", "score" : 25 }
If we want to select documents that matches all conditions then we use the $and
operator.
In the following example we are selecting all the teams from the scoreboard collection that have scored more than 15 points and are active.
Note! For this example I have added the isActive
field to the scoreboard documents.
> db.scoreboard.find({
$and: [
{ "score": { $gt: 15 } },
{ "isActive": true }
]
});
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fc"), "team" : "Watermelon", "score" : 30, "isActive" : true }
{ "_id" : ObjectId("5d16f2b4e8cb73839ac9f2fe"), "team" : "Orange", "score" : 25, "isActive" : true }
We can achieve the same result using the following.
> db.scoreboard.find({
"score": { $gt: 15 },
"isActive": true
});
We use the $or
operator to select documents if any of the given condition is satisfied.
In the following example we are selecting all the teams who are either active or scored at least 10 points. And we are showing the team name, isActive and score.
> db.scoreboard.find({
$or: [
{ "isActive": true },
{ "score": { $gte: 10 }}
]
}, {
"_id": 0,
"team": 1,
"score": 1,
"isActive": 1
});
{ "team" : "Apple", "score" : 20, "isActive" : false }
{ "team" : "Banana", "score" : 15, "isActive" : true }
{ "team" : "Pineapple", "score" : 10, "isActive" : false }
{ "team" : "Watermelon", "score" : 30, "isActive" : true }
{ "team" : "Orange", "score" : 25, "isActive" : true }
In the following example we are fetching all the teams who are active and have either scored 15 or 20 or 25.
We are listing the team name, score and isActive fields.
> db.scoreboard.find({
"isActive": true,
$or: [
{ "score": 15 },
{ "score": 20 },
{ "score": 25 }
]
}, {
"team": 1,
"score": 1,
"isActive": 1,
"_id": 0
});
{ "team" : "Banana", "score" : 15, "isActive" : true }
{ "team" : "Orange", "score" : 25, "isActive" : true }
ADVERTISEMENT