An introduction to MongoDB

This small tutorial will assume you have installed MongoDB previously. Also I will assume you have all the MongoDB client tools installed. We just start with the basic queries.

Basic operations

When the shell command has been started the first thing you want to know is which databases are available. To do this you run the command show dbs. I guess it is an abbreviation of show databases :)

> show dbs
admin
local
crm

The databases admin and local are system databases but the crm database is one I have created. To perform operations on this database we first need to tell we want to use that database.

> use crm
switched to db crm

MongoDB is a lot easier to use than MySQL because when we try to open a non existing database there are no complaints from the server. The catch is only that when there is a typo in your configuration there will be no complaints when using a non existing database or collection.

> use doodle
switched to db doodle

Creating collections and inserting data

Inserting

There is no need to create a collection, these will be created upon first use. With a simple save call the collection will be implicitly created.

> db.persons.save({name: "Leon", phone: "316123456789"})

The save function will accept any javascript object, you even can use different types of objects per inserted row. MongoDB will store the complete object (or document if you like) in a new row in the collection.
Unlike other SQL-servers there is no need to predefine the columns and datatypes you are going to use. So MongoDB is giving us a very flexibel and scalable data storage.

Finding

After inserting the record we can do a find with the following operation. This will returns us the same javascript object we added previously but with an extra field "_id".

> db.persons.find();                                    
{ "_id" : ObjectId("4d83cf306d06a21d02a1b771"), "name" : "Leon", "phone" : "316123456789" }

Also you can use queries to find a record with specific values, more about queries later.
> db.persons.find({"name" : "Leon"});
{ "_id" : ObjectId("4d83cf306d06a21d02a1b771"), "name" : "Leon", "phone" : "316123456789" }

Counting

On the collections you can perform various actions like counting. A count is in fact a function of the db.persons object. So you can call that function like so

> db.persons.count();
1

In these count you can use the same type of queries as in the find.
http://mongodb.onconfluence.com/display/DOCS/Queries+and+Cursors

Updating

When updating documents in MongoDB there are two strategies you can follow. Depending on the type of documents you can either choose the first or the last.

Conventional
The conventional way of updating an object is to fetch the object, change a value and then send the object back to the server. This could become very data intensive when an object is very large or there are very minor changes.

> var row = db.persons.findOne({ name: "Leon"})        
> row.phone = "316987654321"
> row.save()
> db.persons.save(row)

Modifier Operations
MongoDB adds the possibility to update only a few properties of a object at a time. This way you don't have to load the whole object so updating the document is very fast.

> db.persons.update({name: "Leon"}, {$set : {phone: "4567"}})

Update queries can be rather complicated so read the docs on the mongodb website for more information http://www.mongodb.org/display/DOCS/Updating

Removing

Removing rows from the collections is as easy as creating them. As a argument you are giving the same sort of query as when finding or counting. The primary key of a row is the "_id" field so it is logical to use that column when removing rows.

 
> db.persons.remove({"_id": ObjectId("4d83cf306d06a21d02a1b771")}) 

Reacties

Nieuwe reactie inzenden

  • Adressen van webpagina's en e-mailadressen worden automatisch naar links omgezet.
  • Toegelaten HTML-tags: <a> <b> <img> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3>
  • Regels en paragrafen worden automatisch gesplitst.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

Meer informatie over formaatmogelijkheden

Door gebruik van dit formulier accepteert u Mollom's privacybeleid.