Queries with MongoDB

Posted by in MongoDB

In our previous post, I already wrote about MongoDB, a NoSQL database server that today is growing really fast. Here you have seen how to install it and how to start it. Now, let’s see how to work with MongoDB.

On a MS-DOS console, execute mongo:

Iniciando MongoDB

Iniciando MongoDB

If you want to know the databases you have on the server, just write show dbs.

mongodb Show dbs

mongodb Show dbs

The MongoDb console is also a JavaScript console, and because of this, you should be careful with the names you use becasue of lower and upper case. If we try SHOW dbs written of several different forms, all of them give us errors (excepto if we write it on lowercase):

MongoDB & Javascript

MongoDB & Javascript

To use a database, just write the command use manejandodatos, as you do when working with MySQL:

MongoDB - use manejandodatos

MongoDB – use manejandodatos

Let’s check that MongoDB console is a Javascript console as well, just writting a simple math operation:

MongoDB & Javascript

MongoDB & Javascript

Now, let’s create our first documents:

dato1 = { nombre: 'Juan' };
dato2 = { edad: 34 };

And store it on the collection amigos (friends) con save:


MongoDB - Guardando datos

MongoDB – Guardando datos

Let’s show the documents of our friends “amigos“. The SQL sentence should be SELECT * FROM amigos , and the MongoDb sentence is db.amigos.find():

MongoDB - Mostrando documentos

MongoDB – Mostrando documentos

The SQL insert INSERT amigos(fields) VALUES (value) of SQL is transformed to db.amigos.save({ field: value })



Now that we have 3 friends, it’s time to update some information. The update sentence UPDATE amigos SET field = value WHERE condition is equal to db.amigos.update( { condition } , { field: value } )

MongoDB - Actualizando documentos

MongoDB – Actualizando documentos

In the previous image we have modified the information Nombre: Luis by Edad: 32. We hava modified the identifier and the key, even if the types of data are different.

In order to update a document with new properties (or more than one document), you have to include $set, or $unset to remove. Let’s inset a new name without name:

MongoDB - Actualizando documentos

MongoDB – Actualizando documentos

If you want to cound the number of documents, the SQL sentence you need is SELECT COUNT(*) FROM amigos, but in MongoDB you’ll write db.amigos.count():

MongoDB - Agregados

MongoDB – Agregados

To end with, the SQL sentence DELETE FROM tabla is db.amigos.remove() in MongoDB:

MongoDB - Remove

MongoDB – Remove

If you want to re-start a collection instead of removing all documents (in MySQL you’ll use the TRUNCATE sentence), in MongoDB you can do exactly the same with the sentence db.amigos.drop().

The difference between remove and drop is that when using remove the collecion remove one document for operation, so, removing several document requires reveral operations, while drops deletes all document on one operation. For small datasets you won’t find much differences. With large datasets, the results is totally different!

I hope you enjoy playing with MongoDB as much as I did!

Google+ Comments - Comentarios Google+