02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Adding is great, but how do you find things

Finding Data in MongoDB

Now that we've learned how to add data to our MongoDB database by inserting documents into collections, it's time to understand how to retrieve that data efficiently. In this tutorial, we'll go over the basics of querying a MongoDB collection, how to filter and limit results, and how to use some advanced features for better control of your data.

Setting Up

To begin querying data in MongoDB, you need to ensure that your MongoDB server is running. You can do this by typing the following command in a terminal window:

mongod

After the server is running, open another terminal window and enter the MongoDB shell using:

mongo

You can then use the following command to switch to the desired database. In this tutorial, we will use a sample database named MongoSample:

use MongoSample

You should now be in the MongoSample database, ready to begin querying.

Retrieving All Documents

The simplest way to retrieve data from a collection is by using the find() method. For example, if you have a collection named users and want to retrieve all the documents from it, you can use:

db.users.find()

However, the output can sometimes be hard to read, especially if the data is not formatted well. To make the output more readable, use the pretty() method:

db.users.find().pretty()

This will display the data in a structured format, making it easier to understand and navigate.

Querying Specific Documents

Often, you need to find specific documents based on certain criteria. For example, let's say you want to find a document by its unique identifier (the _id field). You can do this by using the ObjectId of the document as follows:

const id = ObjectId("<insert_object_id_here>");
db.users.find({ _id: id }).pretty();

Each document in MongoDB has a unique _id value, which makes it easy to identify and retrieve specific records.

If you want to find documents by a different attribute, such as the user's name, you can use:

db.users.find({ name: "Ben" }).pretty();

Limiting and Skipping Results

If you have a large number of documents in a collection, it might not be practical to retrieve all of them at once. MongoDB offers limit() and skip() methods to control the number of documents that are returned by the find() query.

For example, to retrieve only two documents, use the following:

db.users.find().limit(2).pretty()

If you want to skip the first document and then limit the number of results to two, you can chain the skip() and limit() methods together:

db.users.find().skip(1).limit(2).pretty()

This combination is particularly useful for implementing pagination, where you want to return a specific number of documents on each page.

Important Note on Unique Identifiers

Every document in MongoDB automatically gets a unique identifier called _id. This is very helpful when managing documents, as it makes them easy to find and work with. When you add new documents, MongoDB will automatically assign them an _id unless you specify one yourself.

Summary

In this tutorial, you learned how to find data in a MongoDB collection, how to prettify the output for better readability, and how to filter, limit, and skip documents to get the exact data you need. This process is essential for working with MongoDB and understanding how to retrieve and manage your data effectively.

In the next tutorial, we will explore advanced querying techniques using operators to perform more complicated searches.

Note: MongoDB has introduced several new features since 2016. For example, newer versions offer enhanced capabilities for aggregation and improved indexing options. Ensure that your MongoDB server is updated to take advantage of these newer features.

Video Transcript

00:01Now that we've added content into our database
00:04by adding documents into collections, it's time for us
00:08to learn how to find the items
00:09that we've added into these collections.
00:12To do that, we're gonna go ahead
00:14and search for our content using, well,
00:16finding our content more accurately.
00:19And as we do that, we're gonna have to find a way
00:21to basically filter out these results
00:23and limit the amount of results
00:25that we're gonna be getting back.
00:26And that's exactly what we're gonna do in this lecture.
00:30So in the last lecture we left off
00:32that we've created a database.
00:34And again, right now we're in the database test,
00:37which is not the database we've created
00:39'cause I restarted everything.
00:41So I want to, I want to use, what database do I want to use?
00:43I want to use the Mongo Mongo sample, the one
00:46that I've created, especially for this.
00:48So if I just type db, I'll say, great, I'm inside
00:51of my Mongo sample database.
00:55Now I, I wanna be able to see all the things
00:57that I've created and we saw in the last lecture
00:59that we could add into a collection.
01:01Multiple things. They don't even have to be the same,
01:03they don't even have to have the same identifiers.
01:05They can have different, this is really, really flexible,
01:08but that's what I'm trying to say.
01:09Alright, so let's, yeah,
01:10we created a new collection, which is called users.
01:13And really what I want doesn't want
01:16to get the information that was put into users.
01:18So I'm just gonna type DB users find
01:22and just by typing find
01:23and enter enter, it's gonna output
01:25to me all the different elements.
01:26Now, this is very unorganized for us,
01:28very hard for us to read.
01:29One, things are getting cut, it's not that clean.
01:32A really cool thing that you could do is when you do
01:34that find you could type dot
01:36before you press, press on, enter,
01:38and then type pretty by typing pretty what's gonna happen is
01:42it's gonna basically just prettify the value
01:44that's coming back and make it output
01:46that is easier to read.
01:48And you could see now, by the way, for the first time,
01:50the data that we added in,
01:51we have your three objects, which is true.
01:53We added hen we added Ben, and we added bell.
01:57Well, everyone is almost rhyming
01:58and all has three letters in their name and the ages ranged.
02:02And notice also that one
02:03of our items has a completely different collection,
02:06a different, uh, a property, then the other items.
02:09So you see this flexibility.
02:11Now notice one thing is that every single item here,
02:15every single item has a unique identifier
02:18that's been created dynamically.
02:20Now this unique identifier makes it easy for you
02:22to then approach specific items if you need
02:25to find those specific items.
02:27Now I wanna show you the process involved with that.
02:30So, right, we saw pretty here and we saw find.
02:33Another thing that we could use our our find
02:35to do is we could go and ask to find
02:38specifically whatever we wanna find.
02:40So for example, in this case,
02:41I specifically wanna find this ID the first I
02:45idea, or let's go for the second.
02:46I wanna find Ben.
02:49Technically this is probably not, um, a way
02:52that I would wanna do this
02:53because if I wanna find just one,
02:55I would probably use the find one command. But let's just
02:58For the sake of it, what I've done here,
03:00notice is I've created a new object.
03:02And in that object I've then assigned an ID
03:05and send in this object ID function wrapped around
03:09with the actual ID that we want to fetch.
03:11And once I do this, I'm gonna get that specific item,
03:14which is surprisingly me.
03:16And I could, again, I could make it pretty
03:21and I could see my item in a much prettier way.
03:24Now there's a lot more things that we could do with find,
03:27but I want to go back to that shortly.
03:30I wanna still focus, I wanna still focus on assuming
03:33that we have a lot, a lot of data that is coming in.
03:36So let's go back to that original sample
03:38where we were finding all those items.
03:40And imagine we have your a thousand items.
03:42Now we really don't wanna get all those 1000 items at the
03:46same time in this case.
03:48Really we wanna find, but we only wanna limit that find.
03:51We wanna limit it to, let's say, two items.
03:54By doing that, I'm gonna get back.
03:57I I wrote li limp Limit. Limit, there we go.
04:02And if I limit it to two, then I'm only gonna get two items.
04:05Notice that the two items are based on the order
04:07of the items that were put in.
04:09In this case, I could say, okay, I wanna limit two.
04:11But not only do I wanna limit two,
04:13I actually wanna skip the first item, which is, by the way,
04:16how you would do pagination.
04:18So if you have a hundred items,
04:19you don't wanna load all those 100 items
04:21at the same time, right?
04:23Instead of that, you want to go ahead
04:24and let's say you were gonna limit uh, two,
04:26and I'm oto, I oto.
04:28Wanna, I skip basically one.
04:33And by skipping one, I could say skip two,
04:36I could skip three, but I wouldn't have anything
04:38come back once I skipped three.
04:39So basically I could limit the amount of items
04:41that are coming back and then I could limit the
04:44scope of how much they're coming.
04:46So I could skip basically saying we're gonna start from
04:48after two and we're gonna limit it to two.
04:51So in this case, 'cause we only have three items,
04:52we're only getting the third item that we've added in.
04:56In this lecture, we were introduced to finding things inside
05:01of a MongoDB database.
05:04In the next lecture, we're gonna take it a step further
05:05by finding more complicated, complicated things using
05:11operators.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!