# Getting Started with MongoDB - Part 2

Ready to continue learning about MongoDB, one of the coolest technologies for web developers? In this second part of the series, we move on from the basics, on to advanced queries - with conditional operators - and MapReduce.

## Stepping Beyond the Basics

Earlier, we covered the basics of and how to get started with mongoDB, one of the absolute best of breed of the NoSQL implementations. We looked at how to install it, create a basic database and then perform the basic operations on it:

• search
• update
• delete

In addition to this, we started to look at how to start interacting with mongoDB in a more powerful way, through the use of selectors. Selectors give us the ability to have much more fine grained control and to dig pretty deeply in to find the data that we really want.

Now that's all well and good to get started with, but when you want to write a real application, you need to go a lot further. Well, this is still a getting started series after all, but I want to get you excited about the possibilities of working in a document-oriented way. I want to enthuse you to take this great technology and make it your own and use it as powerfully as you can to make fantastic applications.

Today, we're going to expand on queries from last time and learn two key aspects of mongoDB:

• MapReduce

Previously we looked at basic queries and were introduced to selectors. Now we're going to get into more advanced queries, by building on the previous work in two key ways:

• Conditional Operators
• Regular Expressions

Each of these successively provide us with more fine-grained control over the queries we can write and, consequently, the information that we can extract from our mongoDB databases.

### Conditional Operators

Conditional operators are, as the name implies, operators to collection queries that refine the conditions that the query must match when extracting data from the database. There are a number of them, but today I'm going to focus on 9 key ones. These are:

• $lt – value must be less than the conditional •$gt – value must be greater than the conditional
• $lte – value must be less than or equal to the conditional •$gte – value must be greater than or equal to the conditional
• $in – value must be in a set of conditionals •$nin – value must NOT be in a set of conditionals
• $not – value must be equal to a conditional Let's look at each one in turn. Open up your terminal and get ready to use the original database from the first part in this series (pre-modifications). To make this tutorial easier, we're going to make a slight alteration to the database. We're going to give each document in our collection an age attribute. To do that, run the following modification query: All being well, you can run a 'find all' and you'll have the following output: ###$lt/$lte Now let's find all the actors who are less than 40. To do that, run the following query: After running that query, you'll see the following output: What about the ones who are less than 40 inclusive? Run the following query to return that result: This returns the following list: ###$gt/$gte Now let's find all the actors who are older than 47. Run the following query to find that list: You'll then get the following output: What about inclusive of 40? As there's only one person over 47, the data returned doesn't change. ###$in/$nin What about finding information based on a list of criteria? These first ones have been ok, but arguably, quite trivial. Let's now look to see which of the people we have are either actors or developers. With the following query, we'll find that out (to make it a bit easier to read, we've limited the keys that are returned to just first and last names): This query, yields the following output: You can see that we can get the inverse of this by using $ninjust as simply.

Let's make this a bit more fun and combine some of the operators. Let's say that we want to look for all the people, who are either male or developers, they're less than 40 years of age.

Now that's a bit of a mouthful, but with the operators that we've used so far – quite readily achievable. Let's work through it and you'll see. Have a look at the query below: