MongoDB
In this MongoDB tutorial we will learn about databases, collections and documents.
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.
student_id
first_name
last_name
s0001
Yusuf
Shakeel
If you are familiar with RDBMS (eg. MySQL) then a MongoDB document is like a row in a RDBMS table.
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.
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.
In the following table we can see the mapping between MongoDB and RDBMS.
For example, a table in RDBMS is a collection in MongoDB.
To create database in MongoDB we use the use command followed by the database name.
use
In the following example we are creating a database by the name awesomedb.
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.
To check the currently selected database we use the db command.
db
> db awesomedb
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.
mydb
> use mydb switched to db mydb
If we don't explicitly select a database then by default the test database is selected.
test
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.
To list all the databases we use the show dbs command.
show dbs
> 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.
To drop (delete) database we use the db.dropDatabase() command.
db.dropDatabase()
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.
anotherdb
> use anotherdb switched to db anotherdb > db.dropDatabase() { "dropped" : "anotherdb", "ok" : 1 }
To create a collection inside a database we first switch to that database and then run the db.createCollection("collection-name") command.
db.createCollection("collection-name")
In the following example we are creating users collection inside the awesomedb database.
users
> db.createCollection('users') { "ok" : 1 }
To list all the collections inside a database we use the show collections command.
show collections
> show collections users
To drop (delete) collection we use the drop() method.
drop()
In the following example we are dropping (deleting) the users collection.
> db.users.drop() true
On success the drop() method will return true. Otherwise, it will return false.
true
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 :)