In this section we will go over the basics of connecting, creating, updating and deleting data from a document-oriented database using MongoDB. The following video from MongoDB itself does an amazing job at this:
Note: she talks about cursors, sorting and limits for the multiple find operations. This is quite complex and we will have a special section about this in the advanced operations. For now try to follow along, but don't worry too much if that part goes too fast
{% hyf-youtube src="https://www.youtube.com/watch?v=fbYExfeFsI0" %}
If you want it in written form, there are links in the description of the video.
As there is a lot of different commands used in the above video, we've created a little cheatsheet to keep track of them to link to the documentation.
| command | used for |
|---|---|
| findOne | Find a document. Note that it will return the first document it finds (same as the find function of a JavaScript Array) |
| find | Find all documents that adhere to the filter provided |
| insertOne | Create a new document. Remember that the _id field will be filled in by MongoDB. Overall it is best to just let MongoDB handle all of that! |
| insertMany | Create multiple documents (an array of objects) |
| updateOne | Update the first document that matches the filter |
| updateMany | Update all documents that match the filter |
| deleteOne | Delete a single document that matches the filter |
| deleteMany | Delete all documents that match the filter |
You will have noticed that for a lot of these queries you can provide a filter parameter. This is how to write the WHERE part of your SQL queries. The full documentation for this can be found here but let's look at some common examples and mistakes when working with MongoDB in the context of a database that stores shows, where a show means either a movie or a series:
To find an element by a specific id. Note that we have to use the ObjectId notation as that is what MongoDB uses for its ids.
client
.db("shows")
.collection("movies")
.findOne({
_id: ObjectId("4ecc05e55dd98a436ddcc47c"),
});To find all elements after a specific date. Note the $gte notation, this is a common way for MongoDB to define properties in objects that it does something with. Next to $gte you can use $gt, $lte, $lt to signify greater than, less than or equal or less than respectively. There are many other options, have a look at the documentation.
client
.db("shows")
.collection("movies")
.find({
releasedDate: {
$gte: new Date("2000-01-01"),
},
});To find all elements with a status in a specific list. Here we want to use the $in option, that allows you to specify that a certain field has a value that is one of the ones in the array. This example will return all elements where the status field is either In Production or Post Production.
client
.db("shows")
.collection("movies")
.find({
status: {
$in: ["In Production", "open"],
},
});Projection is the process of defining what fields you want to get back from your query rather than getting the whole document back. This can be very useful to only grab the data you are interested in without having to do this yourself. The full documentation can be found here, let's have a look at an example:
client.db("shows").collection("movies").find(
{},
{
title: true,
tagline: true,
}
);Note that it is the second parameter, so if you still want to find everything you need to provide an empty object in the first parameter.
In this example we are only getting the name and status fields of our database. Now if you run this code you will find that you always get the _id field for free. If you absolutely do not want the _id field then you need to explicitly say you don't want it, like so:
client.db("shows").collection("movies").find(
{},
{
_id: false,
title: true,
tagline: true,
}
);There are many more advanced ways, but those will be for specific use cases. The documentation explains how to do that quite well.
Note that the syntax is slightly different for projection when using the NodeJS driver. So the same example as above would be written as:
client.db("shows").collection("movies").find({}).project({
_id: false,
title: true,
tagline: true,
});All the rules are the same though, so it is just a different syntax. You can find more information about this here.