By Ran Bar-Zik | 2/8/2018 | General |Beginners

MongoDB - Data Retrieval part 2

MongoDB - Data Retrieval part 2

In the previous article we learned about using the find method to find specific values (a name for example), a similar name like we would with LIKE in SQL, or to find a numerical value that is greater than a specific value. Here we’ll continue to examine find. In order to practice with queries we created a simple and straightforward collection with 100 documents.

 

So if in the last article we looked for customers whose value is greater than 95, you should of course be able to figure out that if we want to find the values below 95 we need to to something like this:

db.clients.find({value: {$lt:95}})

If gt means greater than, than lt should mean less than.

Combining two conditions is easy and straightforward. If for instance we want to search for all of the customers with a value between 5-10 we just have to combine the conditions. In this case it would look like:

db.clients.find({value: {$lt:10,$gt:5}})

This is regarding one cell but we can combine—assume we also want customers with values between 5-10 and whose names contain an ‘a’ for example? This is just like how the AND operator works in MySQL.

db.clients.find({name: /a/ ,value: {$lt:10,$gt:5}})

It’s important to keep in mind that we’re talking about the AND conditional. This means that the results contain only those documents whose values are between 5-10 and have an ‘a’ in their name.

But if we want OR? That is to say, documents with a value between 5-10, or that have an a in their name. In this case the syntax changes. We pass to the object the $or attribute. What results is an array in which every member is an OR conditional. It looks like this:

db.clients.find({
   $or: [{
       value: {
           $lt: 10,
           $gt: 5
       }
   }, {
       name: /a/
   }]
})

We can pass as many OR conditionals as we like, with each condition being a member of the array $or. We can also throw in AND with OR like so:

db.clients.find({
   x: {
       $lt: 24
   },
   $or: [{
       value: {
           $lt: 10,
           $gt: 5
       }
   }, {
       name: /a/
   }]
})

So what’s really going on here? For starters, first we have a database that’s we’re using. Second, the collection’s name is clients. Then, the find method that takes our search object. This object is really just a normal conditional that returns only x less than 24, and out of all of them they need to be between 5-10 or have the letter ‘a’ in their name.

 

Confused? Don’t worry. This case is a rather complicated query and pretty theoretical. You can take comfort in the fact the the party starts when we start talking about more complicated objects, but we’ll take one problem at a time. At this point you should know how to retrieve data using find and how to use the OR and AND conditionals. If you need more examples it worth taking a look at the relevant documentation that MongoDB provides.

 

And that’s it! In the next article we’ll talk about more advanced data retrieval with MongoDB: how to use projection, sorting, and limit.

 

Previous article: Data Retrieval: part 1

Next article: Advanced Data Retrieval

 

About the author: Ran Bar-Zik is an experienced web developer whose personal blog, Internet Israel, features articles and guides on Node.js, MongoDB, Git, SASS, jQuery, HTML 5, MySQL, and more. Translation of the original article by Aaron Raizen.

By Ran Bar-Zik | 2/8/2018 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now