MongoDB data retrieval - Part 11
Obviously, once you’ve entered documents into MongoDB, and you’ve updated and deleted, at some point you’ll need to retrieve. And we’ve already seen this very simple, db.products.find() statement, that returns all of the documents currently in our Mongo database, and that’s perfectly fine when we have so few documents, but what happens if instead of five, we have 50 or 500 or 50,000? Not only is returning them all impractical due to the fact that it’s computationally intensive and uses up resources for no good reason, it’s also not of any practical value to the end user. Returning me 50,000 records and asking me to scroll through and visually pick the one I’m interested in is counterintuitive and it’s the exact antithesis of what I expect from a data store. Fortunately, MongoDB allows you to utilize any of these value-pairs as a criteria to search. In other words, I can search for titles, authors, price, page count, or any other values I’ve stored in my documents. Let’s see how that is done.
You also noticed we have some other values in our data store that are not string values, such as “price”. With numeric values, it’s very common for people to wish to use greater than or less than evaluators. Well let’s look at a criteria that searches based on that.
Search using greater than : db.products.find({price:{$gt:50}})
The output returns two values
Search using lessthan : db.products.find({pagecount:{$lt:50}})
The output returns values that are less than 50
Search using equal to : db.products.find({pagecount:{$eq:50}})
The output returns values that are equal to 50