Please read the first guide in this series for more information about MongoDB - Installation, Shell, and Database Management.
There are four major types of Operators in MongoDB. They are:
We'll be looking into just a few operators in this guide. To compare operators, we'll need more fields in our records. Start by adding a few more records and we'll delete them later on.
1db.students.insert([
2 { name: "Bebo", age: 5 },
3 { name: "Chinna", age: 10 },
4 { name: "Elukaludha", age: 15 },
5 { name: "Kaalejoo", age: 20 },
6 { name: "Vela Thedoo", age: 25 },
7 { name: "Kanna Lammoo", age: 30 },
8 { name: "Daydee", age: 40 },
9 { name: "Paati", age: 55 },
10 { name: "Thathaa", age: 60 }
11]);
Executing the above, command we get the following success message:
1> db.students.insert([
2... {"name": "Bebo", "age": 5},
3... {"name": "Chinna", "age": 10},
4... {"name": "Elukaludha", "age": 15},
5... {"name": "Kaalejoo", "age": 20},
6... {"name": "Vela Thedoo", "age": 25},
7... {"name": "Kanna Lammoo", "age": 30},
8... {"name": "Daydee", "age": 40},
9... {"name": "Paati", "age": 55},
10... {"name": "Thathaa", "age": 60}
11... ]);
12BulkWriteResult({
13 "writeErrors" : [ ],
14 "writeConcernErrors" : [ ],
15 "nInserted" : 9,
16 "nUpserted" : 0,
17 "nMatched" : 0,
18 "nModified" : 0,
19 "nRemoved" : 0,
20 "upserted" : [ ]
21})
22> db.students.find();
23{ "_id" : ObjectId("593142968e61243307417cd2"), "name" : "Bebo", "age" : 5 }
24{ "_id" : ObjectId("593142968e61243307417cd3"), "name" : "Chinna", "age" : 10 }
25{ "_id" : ObjectId("593142968e61243307417cd4"), "name" : "Elukaludha", "age" : 15 }
26{ "_id" : ObjectId("593142968e61243307417cd5"), "name" : "Kaalejoo", "age" : 20 }
27{ "_id" : ObjectId("593142968e61243307417cd6"), "name" : "Vela Thedoo", "age" : 25 }
28{ "_id" : ObjectId("593142968e61243307417cd7"), "name" : "Kanna Lammoo", "age" : 30 }
29{ "_id" : ObjectId("593142968e61243307417cd8"), "name" : "Daydee", "age" : 40 }
30{ "_id" : ObjectId("593142968e61243307417cd9"), "name" : "Paati", "age" : 55 }
31{ "_id" : ObjectId("593142968e61243307417cda"), "name" : "Thathaa", "age" : 60 }
32>
Note: Intentionally the other records have been hidden in the previous output.
Now we have a number of names with ages. Let's try to find those who are older than 18. The command and query are as follows:
1db.students.find({
2 age: {
3 $gt: 18
4 }
5});
With the above command, we have the list of students who are aged above 18.
1> db.students.find({"age": {$gt: 18}});
2{ "_id" : ObjectId("593142968e61243307417cd5"), "name" : "Kaalejoo", "age" : 20 }
3{ "_id" : ObjectId("593142968e61243307417cd6"), "name" : "Vela Thedoo", "age" : 25 }
4{ "_id" : ObjectId("593142968e61243307417cd7"), "name" : "Kanna Lammoo", "age" : 30 }
5{ "_id" : ObjectId("593142968e61243307417cd8"), "name" : "Daydee", "age" : 40 }
6{ "_id" : ObjectId("593142968e61243307417cd9"), "name" : "Paati", "age" : 55 }
7{ "_id" : ObjectId("593142968e61243307417cda"), "name" : "Thathaa", "age" : 60 }
8>
The other similar commands are $gte
, $lt
, $lte
, $eq
, $ne
, $in
, $nin
. More information on these can be found here.
What's a database management system without sorting? To sort, we will be using the sort()
function on any cursor (the result of find()
operation and similar operations). The sort()
function takes one object parameter and it has the field names as keys and the values are either 1
or -1
, where they are either ascending or descending. Let's try sorting the students by their name. The command is as follows:
1db.students.find().sort({
2 name: 1
3});
And executing the above, we get everything sorted.
1> db.students.find().sort({
2... "name": 1
3... });
4{ "_id" : ObjectId("593142968e61243307417cd2"), "name" : "Bebo", "age" : 5 }
5{ "_id" : ObjectId("593142968e61243307417cd3"), "name" : "Chinna", "age" : 10 }
6{ "_id" : ObjectId("593142968e61243307417cd8"), "name" : "Daydee", "age" : 40 }
7{ "_id" : ObjectId("593142968e61243307417cd4"), "name" : "Elukaludha", "age" : 15 }
8{ "_id" : ObjectId("593142968e61243307417cd5"), "name" : "Kaalejoo", "age" : 20 }
9{ "_id" : ObjectId("593142968e61243307417cd7"), "name" : "Kanna Lammoo", "age" : 30 }
10{ "_id" : ObjectId("593142968e61243307417cd9"), "name" : "Paati", "age" : 55 }
11{ "_id" : ObjectId("593142968e61243307417cda"), "name" : "Thathaa", "age" : 60 }
12{ "_id" : ObjectId("593142968e61243307417cd6"), "name" : "Vela Thedoo", "age" : 25 }
13>
Let's sort in descending order too:
1> db.students.find().sort({ "name": -1 });
2{ "_id" : ObjectId("593142968e61243307417cd6"), "name" : "Vela Thedoo", "age" : 25 }
3{ "_id" : ObjectId("593142968e61243307417cda"), "name" : "Thathaa", "age" : 60 }
4{ "_id" : ObjectId("593142968e61243307417cd9"), "name" : "Paati", "age" : 55 }
5{ "_id" : ObjectId("593142968e61243307417cd7"), "name" : "Kanna Lammoo", "age" : 30 }
6{ "_id" : ObjectId("593142968e61243307417cd5"), "name" : "Kaalejoo", "age" : 20 }
7{ "_id" : ObjectId("593142968e61243307417cd4"), "name" : "Elukaludha", "age" : 15 }
8{ "_id" : ObjectId("593142968e61243307417cd8"), "name" : "Daydee", "age" : 40 }
9{ "_id" : ObjectId("593142968e61243307417cd3"), "name" : "Chinna", "age" : 10 }
10{ "_id" : ObjectId("593142968e61243307417cd2"), "name" : "Bebo", "age" : 5 }
11>
Blimey! Both work flawlessly.
With the previous example, we were able to sort students based on age, in either descending or ascending order. If we needed to find the oldest of the group, we'd simply sort them descending based on their age and then limit our result to one student.
Similar to the aggregate functions in traditional RDBs, MongoDB has a function called limit()
that can be used on the find()
results AKA the cursor.
The limit()
function takes one parameter, which is the integral value of limiting to the number of results given in it. In our case, it is going to be 1
.
1db.students
2 .find()
3 .sort({
4 age: -1
5 })
6 .limit(1);
Ah, here we go. We get only Thatha
as our senior student.
1> db.students.find().sort({
2... "age": -1
3... }).limit(1);
4{ "_id" : ObjectId("593142968e61243307417cda"), "name" : "Thathaa", "age" : 60 }
5>
There should be a way to count the number of students we have, right? Right. Let's try the count()
on the cursor. To do that, we have the command:
1db.students.find().count();
And wow, we get the nine students:
1> db.students.find().count();
29
3>
Both limit()
and count()
should feel very familiar if you've used MySQL (or any RDB).
We all know that MongoDB is nothing but JavaScript. So, why not use the Array and Object functions on the Database? It's possible using forEach()
method, which can be applied on find()
results.
forEach()
takes a function as its only argument. The current document or result or record will be passed to it. Basically, the function has the following syntax:
1db.students.find().forEach(function(stud) {
2 print("Student Name: " + stud.name);
3});
We can use the concatenation operator like our regular JavaScript. Executing the above, we get:
1> db.students.find().forEach(function (stud) {
2... print("Student Name: " + stud.name);
3... });
4Student Name: Bebo
5Student Name: Chinna
6Student Name: Elukaludha
7Student Name: Kaalejoo
8Student Name: Vela Thedoo
9Student Name: Kanna Lammoo
10Student Name: Daydee
11Student Name: Paati
12Student Name: Thathaa
13>
Clearly, having a JavaScript-like structure makes MongoDB more versatile than RDBs. Whereas MySQL would struggle to perform the task above, MongoDB crushes this problem.
I hope this series of guides have exposed the basics of MongoDB, an increasingly popular NoSQL database that can enhance pretty much any application that relies on using lots of data.
As always, please thumbs up this guide if you enjoyed it. Thanks for reading!