Introduction to MongoDB:Part 2

MongoDB Shell = Build-in MongoDB Client

MongoDB comes with a build in mongodb client called mongo. It is a JavaScript interpreter with build-in MongoDB client capabilities.

starscream-5:bin starscream$ ./mongo localhost:27017
MongoDB shell version: 2.4.4
connecting to: localhost:27017/test
Server has startup warnings:
Mon Jun 24 00:16:12.835 [initandlisten]
> db
test
> db.messagemodels.findOne()
{
    "to" : "Ata Oğuz",
    "subject" : "Furby",
    "body" : "Furby yi aldım oğluş.",
    "_id" : ObjectId("51d01bb444f791b21f000001"),
    "deliveryTime" : ISODate("2013-06-30T11:51:16.126Z"),
    "__v" : 0
}

db variable is set to current database. You can access collections in current database with “.” notation, as in the example above: db.messagemodels.

Creating New Documents and Saving Them

> mail = {to:"test@google.com", from : "Ada Oğuz", subject : "Furby", message : "Ben de pembe Furby istiyorum!!!", deliveryTime:new Date()}
{
    "to" : "test@google.com",
    "from" : "Ada Oğuz",
    "subject" : "Furby",
    "message" : "Ben de pembe Furby istiyorum!!!",
    "deliveryTime" : ISODate("2013-07-02T20:03:46.263Z")
}
> db.messagemodels.insert(mail)

Since this is a JavaScript environment we could create documents a JavaScript objects and assign them to variables as in the above example. After creating the object we could save it using insert method of collection.

Querying Existing Documents

> db.messagemodels.find({from:"Ada Oğuz"})
{ "_id" : ObjectId("51d33242cd860ef98403dc6d"), "to" : "test@google.com", "from" : "Ada Oğuz", "subject" : "Furby", "message" : "Ben de pembe Furby istiyorum!!!", "deliveryTime" : ISODate("2013-07-02T20:03:46.263Z") }
>

You could query existing documents by using find method on the collection. You could supply a simple query also as a document/JS Object. As you can see, an _id field is automatically created and saved with our document.

> use local
switched to db local

You can switch between different databases using use command.

Updating Documents

> newMail = {to:"test@google.com", from : "Ada Oğuz", subject : "Furby", message : "Ben de 1 pembe, 1 tane de mor  Furby istiyorum!!!", deliveryTime:new Date()}
{
    "to" : "test@google.com",
    "from" : "Ada Oğuz",
    "subject" : "Furby",
    "message" : "Ben de 1 pembe, 1 tane de mor  Furby istiyorum!!!",
    "deliveryTime" : ISODate("2013-07-02T21:07:50.743Z")
}
> db.messagemodels.find({from:"Ada Oğuz"});
{ "_id" : ObjectId("51d3417bcd860ef98403dc6e"), "to" : "test@google.com", "from" : "Ada Oğuz", "subject" : "Furby", "message" : "Ben de 1 pembe, 1 tane de mor  Furby istiyorum!!!", "deliveryTime" : ISODate("2013-07-02T21:07:50.743Z") }

To update an existing document, apply your changes to JS Object/document and give it to update function with a criteria that shows which documents should be updated.

Deleting Documents

> db.messagemodels.remove({from:"Ada Oğuz"})
> db.messagemodels.find({from:"Ada Oğuz"});
>

Removing documents is performed with remove method of the collection instance. As in the case of find and update you give it a criteria in the form of a document.

Leave a Reply

Your email address will not be published. Required fields are marked *