Getting Started with mongodb - CodeProject


As known to all, mongodb is powerful and amazing on his scale out. This tip is a guideline to get started with mongodb, hope you can enjoy reading it.

This tip contains four parts:

  1. The basic concepts of mongodb, like document, collection and dynamic schemas
  2. Mongo shell
  3. Customize our prompt with .mongorc.js
  4. Basic operations with mongo shell

For Part #1

Document: An ordered set of keys with associated values, it is equivalent to a row in relational database. And pay attention to the fact that a document couldn't have a duplicate key.

Collection: is a group of documents, it is equivalent to a table in relational database.

Dynamic schemas: The schemas in mongodb is dynamic means the table schema, view schema and so on can be in different shapes.

For Part #2

Before continuing, make sure you have installed the mongodb server on your machine or you have a remote mongodb server to access, or please refer to the below link to get mongodb installed.

After the mongodb has been installed successfully, let's start the mongodb server:

If you use /data/db as the data storing directory, you can start mongodb server through the below command:

$ mongod

or if you have set up your directory for data storing, like me use ~/mongodb, we can start like this:

$ mongod --dbpath ~/mongodb

After the mongodb server has been started, it will listen on port 27017 to wait for your input.

Let's start another shell to interact with mongodb server:

To connect local mongodb server:

$ mongo

Or connect to remote mongodb server:

$ mongo remotehost:port/db

Note: remotehost can be remote host name or remote host IP. 

And we will see the mongo shell version and the current database if our connection is successful.

This shell is also a JavaScript shell, all the JavaScript programs could run here. And I am not planning to dive more into it.

To see the current database or if we want to switch to another database, we can use the below commands:

> db
> use "another database name"

Note: Since the schema in mongodb is dynamic, you do not need to create a db before using it.

And if we want to see which commands are available, we can use the help command:

> help

Explore yourself and you will get more.

And note that when you connect the mongodb, it will load the .mongorc.js which under your home directory. If you know little about .mongorc.js, please look into part #3.

For Part #3

When we do some operations in mongodb shell, what we can see is only a prompt(">"), sometimes we forget what database we currently use, we may think we use dbA currently, but actually we are using dbB, it causes a lot of trouble for us, is there a way to show database information along with the prompt, the answer is definitely.

We can modify .mongorc.js to act exactly as you want, here is a sample to customize our prompt:

    > prompt = function() {
        if (typeof db == 'undefined') {
            return 'nodb>';

        try {
        catch (e) {

        return db+">";

Thus, we can see the database name in front of the prompt. And put the above code into .mongorc.js file, the behaviors will exist every time when you start up the mongo shell.

For Part #4

After the mongo shell is well configured, it's an exciting time to manipulate data now.

This part contains basic operations of mongodb, like insert, find, update and remove.

The insert method:

Let's create a document first:

> info = {"ip" : "",
          "page" : "news",
          "time" : new Date()}

Then do the insert operation:

> db.access.insert(info)

to check whether the info document has been inserted successfully:

> db.access.find()


> db.access.findOne()

to update the existing info document:

> = "movie"
> db.access.update({ip : ""}, info)

to remove the existing info document:

> db.access.remove({ip : ""})

Note that if you write db.access.update({}, info) or db.access.remove({}), all the documents in the collection will be affected.

This tip has reached the end. Thanks for reading and feel free to contact me if you have any questions.