Friday, September 26, 2014

MongoDB CRUD: Part II

Welcome to part two of our excellent adventure. Before we resume, let's seed our database with some more documents to play with.

var max = 33;
while (max--) {
    db.stomach.insert({
        name: 'Oreo',
        part: max % 3 === 0 ? 'white' : 'black'
    });
}

Update

Now that we've got a bunch of Oreos in our stomach collection, I don't think the Sweetwater we added in Part I was such a good idea. Let's change it.

  1. Find the documents we want to update. We'll do that with another query.
  2. Specify the new values. That's done with the update parameter.
db.stomach.update(
    // query
    { beer: 'Sweetwater' },

    // update parameter
    {
        $set: {
            beer: 'root'
        }
    }
);

We changed the document's beer value from 'Sweetwater' to 'root'. This did not affect the other fields in the document, as you can see.

db.stomach.find({
    beer: {
        $exists: true
    }
}).pretty();

{
    "_id" : ObjectId("5424a5ba28f262081e997a7f"),
    "beer" : "root",
    "count" : 2
}

What if we want to completely replace the fields in the document, though? Who wants rootbeer with cookies? Let's make it more complimentary to the other documents in the collection.

db.stomach.update(
    // query
    {
        beer: {
            $exists: true
        }
    },

    // update parameter
    {
        drink: 'milk',
        type: 'skim',
        ounces: 8
    }
);

By specifying a plain object as the update parameter instead of update operators, we've completely changed the document.

> db.stomach.find({ drink: 'milk' }).pretty();
{
    "_id" : ObjectId("5424a5ba28f262081e997a7f"),
    "drink" : "milk",
    "type" : "skim",
    "ounces" : 8
}

Note that the _id does not change when using update, even when replacing the document.

Now, let's change all those old-fashioned Oreos to one of the new flavors.

db.stomach.update(
    // query
    {
        name: 'Oreo',
        part: 'white'
    },

    // update parameter
    {
        $set: {
            part: 'berry'
        }
    }
);

Let's see the results.

> db.stomach.find({ name: 'Oreo', part: 'berry' }).pretty();
{
    "_id" : ObjectId("542603ba21330a1f47f4bd68"),
    "name" : "Oreo",
    "part" : "berry"
}

Only one of the documents was updated. This brings us to the 3rd parameter of update, the options. The options are described in the documentation, one of which is multi.

db.stomach.update(
    // query
    {
        name: 'Oreo',
        part: 'white'
    },

    // update parameter
    {
        $set: {
            part: 'berry'
        }
    },

    // options
    {
        multi: true
    }
);
> db.stomach.find({ name: 'Oreo', part: 'berry' }).count();
11

By default, update only modifies a single document. With multi, we can make it update all the documents matched by our query.

Delete

Now we've got a stomach collection full of berry cookies and milk, the anchovy pizza isn't really sitting well. Let's barf it up.

db.stomach.remove({
    anchovies: {
        $exists: true
    } 
});

That should seem pretty intuitive by now.

References


Check out the accompanying Github repo to these posts about MongoDB/Mongoose.

No comments:

Post a Comment