Querying MongoDB

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.

Searching by ID

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 your own objects:

> db.Products.find(
{
_id: ObjectId("54f8f6b8598e782be72d6295")
})

The response that we will get for our example is as follows:

{ "_id" : ObjectId("54f8f6b8598e782be72d6295"), "name" : "Pear", "price" : 1.5 }

Sure looks like our pear! Now, let's backtrack a bit and see how the lookup works. Note that we essentially did the same thing as we did when we wanted to see all the available Products:

db.Products.find()

However, we qualified what we want to find this time by passing a parameter to find(). As we have grown accustomed to this process by now, the parameter, like most things in MongoDB, is just in JSON:

{ _id: ObjectId("54f8f6b8598e782be72d6295") }

What we do through this parameter is tell MongoDB that we want to find all the documents in the Products collection whose _id property is equal to the corresponding value in our JSON parameter, which is ObjectId("54f8f6b8598e782be72d6295")in this case.

Note that the find() method can return several results. When searching for an ID, this makes little sense, since only one ID can belong to any given document and as such, there can be at the most one result. To accommodate situations like this, MongoDB provides another method for collections—findOne(). It works like find(), with the sole exception being that it returns at most one result, as follows:

> db.Products.findOne({
_id: ObjectId("54f8f6b8598e782be72d6295")
})

Searching by property value

We have seen how easy it is to find documents by ID, and it should come as no surprise that searching by general property values is equally simple. For example, let's say that we want to find all the products with the name Orange. We can do the following:

db.Products.find({"name" : "Orange"})

MongoDB will return the following result:

{ "_id" : ObjectId("54f8f6b8598e782be72d6296"), "name" : "Orange", "price" : 3 }

In some cases, several documents in a collection will have the same value for the property that we are searching for. In that case, MongoDB will return all the matching ones. Here's an example:

db.Products.find({"price" : 3.0})

This will return all the products with a price of 3.0. In our case, it will return the following result:

{ "_id" : ObjectId("54f8f6b8598e782be72d6296"), "name" : "Orange", "price" : 3 },
{ "_id" : ObjectId("54f9b82caf8e5041d9e0af09"), "name" : "Pear", "price" : 3 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.176.166