Getting Started with MongoDB NoSQL: Manipulating Documents

Continuing my previous post on the subject, it’s time to put the theory into practice. My goal now is to explain the basics of the functioning and operation of MongoDB so that then we can apply this to any DBMS programming language (very likely will use Groovy, which fits like a glove).

Installing the server

Of all the non-relational DBMSs have experienced, the one with which I find it easier to work was MongoDB for two reasons: it is really simple and the documentation is excellent. The ease of the system is presented in the first contact we have with the creature: installing it.

Quick and easy: download the version that fits your operating system at this link. Unzip it into a directory of your choice and the animal is almost ready to be used.

To start the server, you must run the program mongod present in the $ MongoDB / bin ($ understand MongoDB as the directory where you unzipped your program distribution). You will be greeted by a friendly error message in which nothing find text similar to the following:

Thu Apr 15 21:32:54 Assertion : 10296 : dbpath ( / data / db / ) does not exist

This is because by default MongoDB stores its databases in the directory / data / db (C: \ data \ db if using Windows) . The solution is simple : either you create this directory or start the process with the mongod -dbpath parameter as in the example below :

$ Mongodb / bin / mongod -dbpath = / home / kicolobo / mongodb

In this case , I am instructing the server to search the database files within the directory / home / kicolobo / mongodb

Note : It is important to mention that if I start MongoDB using the abbreviation ~ for my home directory (eg ~ / mongodb ) the mongod process does not identify the directory , returning that same boring error message (at least on Mac OS X ) .

Important note : if you are using a 32 -bit version of MongoDB , this will have limited the size of the databases to a maximum of 2 GB .


The native client MongoDB is mongo program , located in the $ MongoDB / bin . With the server running, if you start this application automatically you are already logged on to the server default , which is the site using port 27017. It is not necessary to provide a user ( I talk more about user management in a future post though ) . When you start the shell you will be greeted by a prompt similar to the above in the image below :

This can basically do whatever we want with MongoDB. By default , we are connected to the test database. To change the database , just use the use command followed by the database name. For example use kico ;.
manipulating data

In MongoDB , each database consists of a set of collections. The code below , although very simple tells a lot :

> Use kico
switched to db kico
> A = { position : 1, description : ” First letter of the alphabet “}
{ ” Position “: 1 , ” description ” : ” First letter of the alphabet “}
> ( a)
> a
” Position “: 1 ,
” Description ” : ” First letter of the alphabet ”
” _id ” : ObjectId ( ” 4bc7b65da5ec5e132c1a53a1 ” )

The first thing I did was select the kico database using the use command. If this database does not exist, MongoDB automatically create for us.

Then I created a variable called a, whose value is a structure in JSON format consists of two attributes : position and description . Remember that we are dealing here with a schemaless DBMS : therefore can create documents with the format I dream.
( By owning a shell based on JavaScript , you can write scripts and use them for database maintenance tasks . It is a fantastic resource that one day , I want to address in greater detail in this blog. )

To enter my document in a collection , I used the db object, which points to the database currently selected . The syntax for adding a record is simple:

db . [ collection name ] .save ( object to be saved)

Here I am including the document in the alphabet collection. As in the case of the database if this collection does not exist in the current database, it is automatically created when the first document is inserted.

Of course, I could also add a new record in the alphabet collection as in the example below :

1) ( { position : 5 letter ” and”} )

Finally , I exposed the document content after being included in the collection. Note the inclusion of a new attribute in this object : the _id .As the name suggests , an internal identifier used by MongoDB to differentiate objects stored in our collections. It is important to mention that this attribute will only be created by MongoDB where not already available in the structure to be stored . Thus, you are free to create your own identifiers if you want.

Editing and deleting

Edit a record is also easy . Again, let’s look at a simple code that will tell us a lot:

db.alfabeto.findOne document = ( { position : 1} )
documento.bobagem = ” See , I am including a new attribute completely useless ! ” ( document )
db.alfabeto.remove ( document )

The first step was to use the method findOne present in our alphabet collection. This parameter receives as a document fragment and , as its name tells us, returns a single result for us .

Dai forward is simple: add a new attribute to our document and then the persist again in the collection using the same method save () that we saw earlier . The difference here is that a replacement will be made , since the document in question already has an _id attribute.

Important note : you can name that dream to an attribute, but two rules must be satisfied : The attribute name should not start with the $ character and the character can never be included . ‘ ‘ .


In the next post ( I intend to post this week ) will show how to make queries in MongoDB . Does not include this content now because it would make this gigantic post and would not deal with details that desire it.

So , stay tuned for the next post on the subject . Until then!


Please enter your comment!
Please enter your name here