MongoDB 1/n

Last month I attended a MongoDB training in Munich and that's it, I have fallen in love with the noSQL database world. For sure MongoDB is only one in the noSql database world and there are a lot of advantages and disadvantages in this field and especially in the area of MongoDB as well. However, this article series should be for those like me who want to learn something new.

Feel free to start a discussion below I would appreciate very much if I can read your comments.

Some words to my environment:

If you need help with the setup, feel free to use the links to the official documentations:

I want to start with real basic stuff. In this episode I would like to figure out the following items as a starter:

Starting and stopping the MongoDB Server

// switch to your MongoDB install directory and type
// your terminal will be blocked
bin/mongod

// once your MongoDB server ist running, open a new terminal and type mongo
// the mongo shell should be ready to use
mongo

// if your path is not correct, switch to the MongoDB install directory and type
bin/mongo

// if you would like to stop the server
// change your current database and use the shutdown command
use admin
db.serverShutdown()

These are some basic commands to start and stop the MongoDB server. One more useful command at this time is serverStatus. This command will show you a lot of information about the server, in particular the current server version you are running.

// change to the admin db
use admin

// show some server stats, especially your server version
db.serverStatus()

To change between the databases you have to use the use command.

Playing with the mongo shell

The MongoDB shell is your basic interface to communicate with the database and it is a full javascript interpreter.

Get some help

help

Show all databases

This command gives you an overview about the databases.

db

Use a database

This command switches to the given database

use test

Show all collections in a particular database

This command will display all collections in the given database. The term collection is a key point in MongoDB.
A collection stands for a table in the relational database world.

show collections

Insert your first document

If you insert the first document in your database the collection is automatically created by MongoDB. Your first insert for this scenario could be something like that. If you insert the first document the database (episode1) will also be created by MongoDB.

// switch or create the database
use episode1

// insert the first document
db.article.insert({title:"The man on the moon", added:new Date()})
// db will show you the actual database you use
db

//  show collections will show you the new collection (table) article
show collections

Make your first query

To query your data you have to use the find() command.

// will display all documents in the collection article
db.article.find();

// will display the same but more clearly
db.article.find().pretty();

Your result should be like this:

db.article.find().pretty()
{
    "_id" : ObjectId("538b23cd1f71d2e5f9ce0abe"),
    "title" : "The manon the moon",
    "added" : ISODate("2014-06-01T12:59:57.347Z")
}

As you can see there are three keys in this document. _id, title and added. The last two we have inserted. The _id key is a special one. This key is the so-called primary key in the relational world and is created automatically by MongoDB. In the next episode we will play a little bit more with the find command. For now let's see how we can drop a document or a collection.

Delete your first document

To delete the first document we can use the remove command as you can see below. In further episodes we will see another possibility to delete documents, the so-called delete command which is introduced in MongoDB in version 2.6.

db.article.remove({_id:ObjectId("538b23cd1f71d2e5f9ce0abe")})
Now you will have an empty collection as you can see it with the command:
db.article.stats())

The key count shows you 0, that means there is no document in this collection (table). This key represents the number of documents in the given collection.

With this topic I would like to close this first flight with MongoDB. In the next episode I will cover the CRUD operations in MongoDB.


Ich freue mich auf Ihren Anruf
+43(0)664 - 23 00 365

Folgen Sie mir