Intermediate Mongo

Lesson Objectives

Let's go into a few more complex examples of how Mongo can be used. We'll cover the following:

  1. Using intermediate find operators

  2. Explaining update operators

  3. Explaining upserts

  4. Explaining multiple updates

Use intermediate find operators

To find all the documents in our collection that have a weight property greater than 700, we run:

db.employees.find(
    {
        weight : {
            $gt : 700
        }
    }
)

There are several comparisons we can perform

  • $lt (less than)

  • $lte (less than or equal to)

  • $gt (greater than)

  • $gte (greater than or equal to)

  • $ne (not equal to)

  • $exists (does the property exist on an object -- either true or false)

  • $in (does the value exist within the given array)

If the field is an array, you can search for a match within that array

If the object you pass into find() has more than one attribute, it will return documents that match both criteria. This is called an AND statement (like &&)

To find all documents that match at least one of a set of criteria, use an OR statement (like ||)

To find documents that have a value that matches multiple criteria, pass an object that contains both tests. This is similar to an AND (&&), but for one property. If you try to do a normal AND statement, but use the same property, twice it won't work.

Explain intermediate update operators

We can increase a specific value

Multiple a value

Push a value onto an array

Pop a value off an array

Remove a property altogether

Rename a field

For more operators, look here: http://docs.mongodb.org/manual/reference/operator/update/#update-operators

Explain upserts

Upserts will insert a value if it doesn't exist. If it does, it will update it.

Normal:

Upsert:

Summary

MongoDB is quite versatile and gives the developer a lot more freedom than SQL. What would easily break a SQL database - like suddenly removing or renaming a column - are totally fine in a Mongo database!

Last updated

Was this helpful?