MongoDB - Database and Collection


mongodb logo - tutorial - dyclassroom

In this MongoDB tutorial we will learn about databases, collections and documents.

What is a document?

A document is a key value pair. It is a JSON (JavaScript Object Notation).

Following example represents a student document.

  "student_id": "s0001",
  "first_name": "Yusuf",
  "last_name": "Shakeel"

student_id, first_name and last_name are referred as the fields or keys of the document and s0001, Yusuf and Shakeel are the respective values of the given fields.

If you are familiar with RDBMS (eg. MySQL) then a MongoDB document is like a row in a RDBMS table.

What is a collection?

A collection is a set of documents.

A single collection can have zero or more number of documents.

Think of MongoDB collections as tables in RDBMS.

What is a database?

A database is an organised set of collections.

A single MongoDB database can have one or more number of collections.

A single MongoDB server can have one or more number of databases and each of the database is saved as a file.

MongoDB and RDBMS

In the following table we can see the mapping between MongoDB and RDBMS.

For example, a table in RDBMS is a collection in MongoDB.

Database Database
Collection Table
Document Row
Field Column
Embedded Document Join Table
Primary key (default _id) Primary key

Creating database

To create database in MongoDB we use the use command followed by the database name.

In the following example we are creating a database by the name awesomedb.

> use awesomedb
switched to db awesomedb

If the database exists then it will be ready for use. If the database does not exists then it gets created.

Show the currently selected database

To check the currently selected database we use the db command.

> db

Switch to a database

To switch to a database we also use the use command.

In the following example we are switching to a database by the name mydb.

> use mydb
switched to db mydb

If we don't explicitly select a database then by default the test database is selected.

To check this just log out and re-login to your MongoDB server and without selecting any database run the command db and it will show you test as the selected database.

So, before starting your work make sure you have selected the database you wanted.

List all the databases

To list all the databases we use the show dbs command.

> show dbs
mydb             0.001GB
anotherdb        0.023GB

If you have created a database and it has nothing inside then it will not be listed when you run the show dbs command.

Drop database

To drop (delete) database we use the db.dropDatabase() command.

The db in the above command represent the currently selected database.

In the following example the currently selected database is anotherdb. So, to drop (delete) it we will run the db.dropDatabase() command.

> use anotherdb
switched to db anotherdb
> db.dropDatabase()
{ "dropped" : "anotherdb", "ok" : 1 }

Create collection

To create a collection inside a database we first switch to that database and then run the db.createCollection("collection-name") command.

In the following example we are creating users collection inside the awesomedb database.

> db.createCollection('users')
{ "ok" : 1 }

List all collections

To list all the collections inside a database we use the show collections command.

> show collections

Drop collection

To drop (delete) collection we use the drop() method.

In the following example we are dropping (deleting) the users collection.

> db.users.drop()

On success the drop() method will return true. Otherwise, it will return false.

Alright, we now know how to create and drop databases and collections. In the next tutorial we will learn to work with documents.

See you in the next tutorial. Don't forget to share this tutorial. Have fun developing :)