MongoDB basics for everyone – Part 6 – modifiers and operators

In order to demonstrate the selector and modifier behaviour in MongoDB, we will insert a slightly more complex document to experiment on.

db.testcollection.insert({"name":"Rapheal", "artist": true, "ninjaturtle": true})
db.testcollection.insert({"name":"Michelangelo", "artist": true, "ninjaturtle": true})
db.testcollection.insert({"name":"Donatello", "artist": true, "ninjaturtle": true})
db.testcollection.insert({"name":"Leonardo", "artist": true, "ninjaturtle": true})
db.testcollection.insert({"name":"Picasso", "artist": true, "ninjaturtle": false})
db.testcollection.insert({"name":"Monet", "artist": true, "ninjaturtle": false})

You can pass in certain parameters to both find() and findOne() in order to select specific documents in the store. Let’s make an example of this by only selecting the documents that are about Ninja Turtles from above.

If you were to do a full find() on this collection, you should get the following back:

> db.testcollection.find()
{ "_id" : ObjectId("51ef62b7305e05be29bf242c"), "name" : "Rapheal", "artist" : true, "ninjaturtle" : true }
{ "_id" : ObjectId("51ef62b7305e05be29bf242d"), "name" : "Michelangelo", "artist" : true, "ninjaturtle" : true }
{ "_id" : ObjectId("51ef62b7305e05be29bf242e"), "name" : "Donatello", "artist" : true, "ninjaturtle" : true }
{ "_id" : ObjectId("51ef62b7305e05be29bf242f"), "name" : "Leonardo", "artist" : true, "ninjaturtle" : true }
{ "_id" : ObjectId("51ef62b7305e05be29bf2430"), "name" : "Picasso", "artist" : true, "ninjaturtle" : false }
{ "_id" : ObjectId("51ef62b9305e05be29bf2431"), "name" : "Monet", "artist" : true, "ninjaturtle" : false }

However, our example will be to select back only the Ninja Turtles in the collection, so we pass in the “ninjaturtles” selector to the find command:

> db.testcollection.find({"ninjaturtle":true})
{ "_id" : ObjectId("51ef62b7305e05be29bf242c"), "name" : "Rapheal", "artist" : true, "ninjaturtle" : true }
{ "_id" : ObjectId("51ef62b7305e05be29bf242d"), "name" : "Michelangelo", "artist" : true, "ninjaturtle" : true }
{ "_id" : ObjectId("51ef62b7305e05be29bf242e"), "name" : "Donatello", "artist" : true, "ninjaturtle" : true }
{ "_id" : ObjectId("51ef62b7305e05be29bf242f"), "name" : "Leonardo", "artist" : true, "ninjaturtle" : true }

In this way, you can make your queries return fewer results and ensure that indexes are used correctly. This will also work on the “dynamic schema” that we spoke about earlier, so, if for example, you wanted to add “sculptor” to the Michelangelo field (yes, I know about the others, this is not an art holy war, it is a Mongo example…) I could then get back all of the sculptors, who are also ninjaturtles.

First we will introduce the $set command modifier to update the “Michelangelo” document:

db.testcollection.update({"name": "Michelangelo"}, {$set:{"sculptor":true}})

As you can see, we did not need to do any other schema changes, or have to add a NULL or something to all the other documents, Mongo allows us to carry on as we were:

db.testcollection.find()
{ "_id" : ObjectId("51ef62b7305e05be29bf242c"), "name" : "Rapheal", "artist" : true, "ninjaturtle" : true }
{ "_id" : ObjectId("51ef62b7305e05be29bf242e"), "name" : "Donatello", "artist" : true, "ninjaturtle" : true }
{ "_id" : ObjectId("51ef62b7305e05be29bf242f"), "name" : "Leonardo", "artist" : true, "ninjaturtle" : true }
{ "_id" : ObjectId("51ef62b7305e05be29bf2430"), "name" : "Picasso", "artist" : true, "ninjaturtle" : false }
{ "_id" : ObjectId("51ef62b9305e05be29bf2431"), "name" : "Monet", "artist" : true, "ninjaturtle" : false }
{ "_id" : ObjectId("51ef62b7305e05be29bf242d"), "artist" : true, "name" : "Michelangelo", "ninjaturtle" : true, "sculptor" : true }

You will now notice that the Michelangelo document has an additional field. We can then select (with modifiers) all the artists that are Ninja Turtles as well as sculptors:

db.testcollection.find({"ninjaturtle": true, "sculptor":true})

Which will gve us:

{ "_id" : ObjectId("51ef62b7305e05be29bf242d"), "artist" : true, "name" : "Michelangelo", "ninjaturtle" : true, "sculptor" : true }

The other documents are ignored due to failing the criteria for selection, and of course, you see no errors due to schema mismatches! Great!

Let me just quickly now go through a few of the other modifiers that are commonly used in MongoDB.

$inc – increment a field integer value by one. e.g.
Let’s take the following document as our example:

db.monkeys.insert({"name":"vervet", "population":2})
db.collection.update({field:value}, {$inc:{field1: amount}})

So in orde to update our “monkeys” collection, we can simply increment the population field:

db.monkeys.update({"name":"vervet"}, { $inc: {"population":1}})

which will result in

{ "_id" : ObjectId("51ef691c305e05be29bf2432"), "name" : "vervet", "population" : 3 }

The only other operators that I would like to outline here are $set and $unset. $set sets an item in much the same way that the SET keyword does in SQL, while $unset does the opposite.

As an example:

We suddeny have a new family of vervet monkeys moving into our collection, so instead of incrementing the population with arbitrary numbers, we simply $set the population number

db.monkeys.update({"name":"vervet"}, { $set: {"population":7}})
db.monkeys.find()
{ "_id" : ObjectId("51ef691c305e05be29bf2432"), "name" : "vervet", "population" : 7 }

When $unset is used, the syntax is very similar:
We will take the example that we have discovered that not only vervet monkeys live in the area, so we must unset the name field:

db.monkeys.update({"name":"vervet"}, { $unset: {"name":""}})
db.monkeys.find()
{ "_id" : ObjectId("51ef691c305e05be29bf2432"), "population" : 7 }

Please keep in mind that these are very simple examples to demonstrate the principals behind the documents!

Summary
Author Rating
4
Software Name
MongoDB
Operating System
all
Price
0