Tags

, , , , ,

It’s time to create your first document in MongoDB.

(I am considering that you are going to work with MongoDB for the first time).

Since you’re using a JavaScript shell, your documents will be specified in JSON (JavaScript Object Notation).

For instance, one of the documents describing a user might look like this:

{username: “manoj”}

The document contains a single key and value for storing Manoj as username.

To save this document, you need to choose a collection to save it to. Here’s how (type the following on shell prompt and hit enter key):

> db.users.insert({username: “manoj”})

If the above insert is succeeded, then you just have saved your first document on mongoDB.

You can issue a simple query to verify that the document has been saved:

> db.users.find()

The result will look something like that:

{ “_id” : ObjectId(“53d8dd15e7d002357d124b42”), “username” : “manoj” }

Note that an _id field has been added to the document, which acts as the document’s primary key. Every MongoDB document requires an _id, and if one isn’t present when the document is created, then a special MongoDB object ID will be generated automatically and added to the document at the time of record building.

I will talk about object IDs in a later tutorial in this series, first let us add some more users (e.g. John, Madonna, Michael, Manu):

> db.users.save({username: “John”})
> db.users.save({username: “Madonna”})
> db.users.save({username: “Michael”})
> db.users.save({username: “Manu”}

There are now 5 users in the documents and you can count them using:

> db.users.count()

5

And also you can find them using (like earlier):

> db.users.find()

{ “_id” : ObjectId(“53d8dd15e7d002357d124b42”), “username” : “manoj” }
{ “_id” : ObjectId(“53d8e215e7d002357d124b43”), “username” : “John” }
{ “_id” : ObjectId(“53d8e21ee7d002357d124b44”), “username” : “Madonna” }
{ “_id” : ObjectId(“53d8e224e7d002357d124b45”), “username” : “Michael” }
{ “_id” : ObjectId(“53d8e22ae7d002357d124b46”), “username” : “Manu” }

You can also pass the simple query to find specific user:

> db.users.find({username: “Manu”})

{ “_id” : ObjectId(“53d8e22ae7d002357d124b46”), “username” : “Manu” }

Updating documents

You can update the documents using at least two arguments, first specifies which document to update and second specifies how the selected document be updated.

For instance, suppose you want to modify user “manoj” by adding his country. You can do this as follows:

> db.users.update({username: “manoj”}, {$set: {country: “India”}})

Suppose you also want to modify user “manoj” with “Manoj”, you can do as follows:

> db.users.update({username: “manoj”}, {$set: {username: “Manoj”}})

Now search:

> db.users.find({username: “manoj”})

Nothing found!

Now, try:

> db.users.find({username: “Manoj”})

This return the following:

{ “_id” : ObjectId(“53d8dd15e7d002357d124b42”), “username” : “Manoj”, “country” : “India” }

Let’s also update the document for “Madonna” by adding her country “USA”:

> db.users.update({username: “Madonna”}, {$set: {country: “USA”}})

> db.users.find()

{ “_id” : ObjectId(“53d8e215e7d002357d124b43”), “username” : “John” }
{ “_id” : ObjectId(“53d8e224e7d002357d124b45”), “username” : “Michael” }
{ “_id” : ObjectId(“53d8e22ae7d002357d124b46”), “username” : “Manu” }
{ “_id” : ObjectId(“53d8dd15e7d002357d124b42”), “username” : “Manoj”, “country” : “India” }
{ “_id” : ObjectId(“53d8e21ee7d002357d124b44”), “username” : “Madonna”, “country” : “USA” }

One more thing. She later decides, she doesn’t want her country to be shown in the documents. You can remove that by “unsetting” the country:

> db.users.update({username: “Madonna”}, {$unset: {country: 1}})

> db.users.find({username: “Madonna”})

{ “_id” : ObjectId(“53d8e21ee7d002357d124b44”), “username” : “Madonna” }

Let’s do some more things with this example.

Let’s suppose that, in addition to storing profile information, users also wnat to store lists of their favorite things.

And such sample document representation might look something like this:

{ username: “Manoj”,
    favorites: {
            cities: [“New Delhi”, “New York”, “Jakarta”],
            movies: [“MI3”, “Spiderman”, “The Perfect Storm”, “Enemy at the gates”]
            food: [“Samosa”, “Chicken Curry”, “Kebab”, “Biryani”]
           }
}

Since you are updating the document, you can use $set as before:

> db.users.update({username: “Manoj”}, {$set: {favorites: {cities: [“New Delhi”, “New York”, “Jakarta”], movies: [“MI3”, “Spiderman”, “The Perfect Storm”, “Enemy at the gates”], food: [“Samosa”, “Chicken Curry”, “Kebab”, “Biryani”]}}})

Returns….

WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })

Now query:

> db.users.find()

Return:

{ “_id” : ObjectId(“53d8e215e7d002357d124b43”), “username” : “John” }
{ “_id” : ObjectId(“53d8e224e7d002357d124b45”), “username” : “Michael” }
{ “_id” : ObjectId(“53d8e22ae7d002357d124b46”), “username” : “Manu” }
{ “_id” : ObjectId(“53d8e21ee7d002357d124b44”), “username” : “Madonna” }
{ “_id” : ObjectId(“53d8dd15e7d002357d124b42”), “username” : “Manoj”, “country” : “India”, “favorites” : { “cities” : [ “New Delhi”, “New York”, “Jakarta” ], “movies” : [ “MI3”, “Spiderman”, “The Perfect Storm”, “Enemy at the gates” ], “food” : [ “Samosa”, “Chicken Curry”, “Kebab”, “Biryani” ] } }

Suppose that you want to find all users who like the movie “MI3”, you can query:

> db.users.find({“favorites.movies”: “MI3”})

This shows the document only one document because you have only one such document:

{ “_id” : ObjectId(“53d8dd15e7d002357d124b42”), “username” : “Manoj”, “country” : “India”, “favorites” : { “cities” : [ “New Delhi”, “New York”, “Jakarta” ], “movies” : [ “MI3”, “Spiderman”, “The Perfect Storm”, “Enemy at the gates” ], “food” : [ “Samosa”, “Chicken Curry”, “Kebab”, “Biryani” ] } }

Please add data for some more users and play with different query. For example:

 > db.users.update( {username: “Madonna”}, {“$set”: {favorites: { movies: [“Casablanca”, “Rocky”, “MI3”] }}})

Now run the query:

> db.users.find({“favorites.movies”: “MI3”})

Will return:

{ “_id” : ObjectId(“53d8dd15e7d002357d124b42”), “username” : “Manoj”, “country” : “India”, “favorites” : { “cities” : [ “New Delhi”, “New York”, “Jakarta” ], “movies” : [ “MI3”, “Spiderman”, “The Perfect Storm”, “Enemy at the gates” ], “food” : [ “Samosa”, “Chicken Curry”, “Kebab”, “Biryani” ] } }
{ “_id” : ObjectId(“53d8e21ee7d002357d124b44”), “username” : “Madonna”, “favorites” : { “movies” : [ “Casablanca”, “Rocky”, “MI3” ] } }

That’s all folks for now…

I will come back another tutorial in this series soon…

Currently I am working on R and MongoDB, which is really interesting for web analytics. I thought of writing tutorials in this area and so why this is how I am starting.

Good Luck!

Manoj

Advertisements