We are now familiar with the overall structure of data storage in MongoDB as well as how to insert and perform some rudimentary retrieval using the find()
method. Here, we will look at the more advanced usage of find()
in order to do some more fine-grained data retrieval.
One of the most common operations on a MongoDB instance is lookups based on ID. As you may recall, every document in a database has a unique _id
field, and MongoDB makes it easy to find documents using it.
Let's try this out! Start your Mongo shell and open the OrderBase
database again. If you closed it after the last example, you can reopen the database by issuing the following command:
> use OrderBase
Once the database has been selected, let's say that we want to look up a given product by ID. We select an ID from the earlier example at random and see what we end up with. Remember that the ID will be different on your own machine. So, you will need to select the one that is associated with...