MongoDb Databases, Collections and Documents Explained – MongoDb Part 2

Welcome back for another post as promised on more about mongodb. If you missed part one of the series check it out here

The objectives of this post include.

  1. Explain mongodb in relation to a relational database for a better understanding
  2. What is a mongo database
  3. What are mongodb collections
  4. How to create a collection
  5. What are documents?
  6. How to create a document

Explain mongodb in relation to a relational database for a better understanding.

Being from a relational database, its of great importance to set some of the aspect clear. In a relational database, data is organized in records, in a database table. Basically in RDMS we have tables and rows.

In mongodb or nosql database, the data is structured in collections and documents. In mongodb the documents are just but JSON which is simply a key-value pair fields.JSON is also a natural data format for use in the application layer. JSON supports a richer and more flexible data structure than tables made up of columns and rows. In addition to supporting field types like number, string, Boolean, etc., JSON fields can be arrays or nested sub-objects. This means we can represent a set of sophisticated relations which are a closer representation of the objects our applications work with.

Creating a mongo database.

You can create a database even without using it. Simply use the following command on CLI

    use accounts

This is illustrated in the following snapshot

By doing this we have created a database, but remember that mongo will not create the database until you create a collection in it.


Mongodb collection are the containers that holds the documents. Mongodb creates a collection implicitly when you first reference  a collection in a command. In the following figure I am going to connect to my mongo instance, and show you what I mean by this.

Implicitly create a collection

I have tried to be explicit as possible in describing the above image.Of importance is step 4 where we reference unexisting student collections and insert a document, and mongo creates students collection automatically.

There are other collections called capped collection. By definition from mongo documentation, this is a fixed sized collection that automatically overwrites its oldest entries when it reaches its maximum size. This sort of collection can easily be created with createCollection

  db.createCollection(name, options)

The options parameter is optional and its document (json) in the following format

{ capped: <boolean>,
  autoIndexId: <boolean>,
  size: <number>,
  max: <number>,
  storageEngine: <document>

Of importance here is the ‘capped’ boolean, which indicates that the collection is capped. If this is set to true then ‘size’ is also required to be provided. In the following figure I am creating a capped collection called “Subjects”

Simply those are the few and quick ways to creating collections for mongo.


When you create a collection implicitly mongodb does not create the collection per se. For the collection  to be available you need to insert a document into it. Documents are the just json objects (key-value pairs) which are contained in a collection.

Mongodb has various methods to adding new documents to a collection.

One of the method is db.collection.insert();

     name: "name1",
     accounts: [ { name: "Savings", balance: 25 }, { name: "Fixed", balance: 50 } ],
     category: "junior"

The above operation return, showing how many documents have been inserted..

   WriteResult({ "nInserted" : 1 })

Also a quick note is that you can pass a collection to insert and it will insert all the  documents, but will return the following result on success. We will discuss this and bulk insert in a future post. 🙂

   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 3,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]

The other option to adding documents to our collection is using This operation updates an existing document or inserts a new document. If the document passed to this operation has ‘_id’ parameter then result would be an update by getting the document with the ‘_id’ and replacing all fields in the existing record with the fields in the document, and if the document does not exist with the specified ‘_id’ it perfoms an insert. The write concern can be taken as being synomous more to isolation levels
     writeConcern: <document>


There is alot to write about this topic, but lets end it here for now. There will be more on future posts. In this post we have been able to cover on how to create mongo database, collections and documents. We also quickly and simply differentiated on ‘save’ and ‘insert’ operations for documents, so you know how , when and where to use them.

Feel free to drop a comment or drop an email with your concerns and views on the topic.

Stay tuned for part 3 -Happy coding!! 🙂


Tags: ,

About Piusn

Enthusiastic Software Developer

What's your thought on the subject?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: