Introduction To MongoDB Atlas

Nowadays MongoDB is one of the most popular NoSQL databases. NoSQL means 'not only SQL' , and as the the name suggests, we are not managing data using traditional RDBMS. Before jumping into MongoDB, let’s find out different NoSQL databases and their types.
 

Types of NoSQL databases

 
There are 4 basic types of NoSQL databases:
  1. Key-Value-Based Database -- Big Hash Table of keys & values

    Example: Dynamo (Amazon S3)

  2. Document-based Database -- Stores documents with elements.

    Example: CouchDB, MongoDB

  3. Column-based Database -- Stores data in one big table like Excel.

    Example: Cassandra and HBase

  4. Graph-based Database -- Graph database is a technology for data management designed to handle very large sets of structured, semi-structured or unstructured data.

    Example: Neo4J
Out of these many options we are going to concentrate only on MongoDB.
 
So, let’s figure out how we need to manage the data using MongoDB.
 
So now we can start to understand the basics of MongoDB and also will have some insights into practical work as well.
 
MongoDB has the following hierarchy. Below is a comparison with RDBMS hierarchy.
 
MongoDB Atlas 
 

Environment Setup

 
We  have two options available
  1. Local Setup
    For this purpose, we need to download and install MongoDB installer and then we need to run Mongodb command line tools to start/stop the server.

    In this article we will focus on cloud setup.

  2. Cloud Setup
    MongoDB cloud setup is known as MongoDB Atlas. Let’s start practically with MongoDB Atlas. As Atlas is the cloud version of MongoDB there is  no need to download or install anything on your local box so please open the browser and add this url.

    Then you will see the below page where you can easily find out the button “Launch a Free Cluster”. Just click that button.

    MongoDB Atlas 
After selecting “Launch a Free Cluster” you will see the registration page. If you are already registered then go back there and you will be able to see “Log in here”
 
MongoDB Atlas 
 
After the registration is complete:
 
MongoDB Atlas 
 
Here we will get three options and we will go with free option and select “Create a cluster” 
 
MongoDB Atlas 
 
Atlas gives us three cloud options AWS, Google Cloud and AWS options.
 
MongoDB Atlas 
 
I have selected AWS and Mumbai option and clicked “Create Cluster” button.
 
MongoDB Atlas 
 
To create cluster, it will take couple of minutes. 
 
MongoDB Atlas 
 
Get “Database Access” – Select Database Access
 
MongoDB Atlas 
 
Add New User:
 
MongoDB Atlas 
 
While creating user we can set privileges as well. I am selecting default privileges for admin user.
 
Get “Network Access” – Select Network Access
 
MongoDB Atlas 
 
Select button. Add IP Address then you will get a popup:
 
MongoDB Atlas 
 
Select ALLOW FROM ANYWHERE OPTION and click Confirm
 
MongoDB Atlas 
 
After configuring the security part, select cluster from left panel.
 
MongoDB Atlas 
 
Now select Collection Button
 
MongoDB Atlas 
 
Here we will get two options -  readymade dataset or user can create his/her collection and add data. If you select Load a Sample Dataset it will add 350 mb dataset so I am selecting add my own data:
 
MongoDB Atlas 
 
You can give whatever database name you wish, collection name ,and click Create Button.
 
MongoDB Atlas 
 
After creating database and collection then the next step is to add document using INSERT DOCUMENT.
 
MongoDB Atlas
 
MongoDB Atlas
 
So far we have finsihed the basic setup of MongoDB atlas and how to create the database -- now we will move forward to understand more about MongoDB.
 
MongoDB is storing data in the form of json. This json is known as BSON. BSON stands for binary JSON. See the below comparison between JSON and BSON.
 
Json
Bson
Json is file format
Binary json
Slower
Faster than json
Encoding and decoding techniques are not available
Encoding and decoding techniques are not available
 
Primary key is required while inserting a new document. In MongoDB this primary is key known as _id field. The _id field contains a unique ObjectID value. When we are inserting new document _id gets automatically gets generated. We can also override this unique objectID by passing manually _id as field while inserting new document.
 

Access MongoDB Atlas externally

 
Select cluster from left panel and then click CONNECT button
 
MongoDB Atlas 
 
You will see popup with three options. We will explore each option
 
MongoDB Atlas 
 
So, first select “Connect with Mongo Shell” – This option provides us with the facility to connect MongoDB with one local console environment.
 
MongoDB Atlas 
 
Here you need to follow these 3 steps. In my case I am selecting Windows and copying the command as I have already installed Mongo Shell locally. So I am opening my mongo shell locally and running the following mongo command:
 
mongo "mongodb+srv://cluster0-kbpys.mongodb.net/test" --username admin 
 
MongoDB Atlas 
 
After running this command, we need to provide the password:
 
MongoDB Atlas 
 
Here we can add commands to retrieve database, collections and documents from MongoDB atlas and also, we create new database, collections and also insert new database.
 
Now we try explorer commands,
  • show dbs - List all databases

    MongoDB Atlas
  • use <database_name> – Change to database and if that database does not exist then create a new database with that name.
  • db – Show current database.

    MongoDB Atlas
In my case ‘test’ is my current database.
 
Now I am creating new database: OrganizationDB
 
MongoDB Atlas
 
MongoDB Atlas 
 
In this list we are not getting OrganizationDB. It is because until we are adding any collections it is not showing in list.
 
Let's find some more commands
  • Show collections
    1. // List down collections of the current database  
    2. MongoDB Enterprise Cluster0-shard-0:PRIMARY> show collections;   
    3. MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.getCollectionNames();  
  • Create new collection
    1. db.createCollection("collection-name");  
  • Insert new document in collection.
    1. // Insert single document  
    2. MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.<collection_name>.insert({field1: "value", field2: "value"})  
    3. // Insert multiple documents  
    4. MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.<collection_name>.insert([{field1: "value1"}, {field1: "value2"}])  
    5. MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.<collection_name>.insertMany([{field1: "value1"}, {field1: "value2"}])  
    6. (Note: If you insert the record with collection_name and if that collection is not available then automatically collection will get created.)     
  • Find data from collection
    1. // Find all records  
    2. MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.< collection_name>.find();  
    3. // Retrieve limited number of records  
    4. db.< collection_name>.find().limit(10);  
    5. // Retrieve records on condition  
    6. db.< collection_name>.find({"_id": ObjectId("someid")});  
    7.   
    8. // Sample queries   
    9. MongoDB Enterprise Cluster0-shard-0:PRIMARY> show dbs  
    10. SampleDB  0.000GB  
    11. admin     0.000GB  
    12. local     4.610GB  
    13. MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.City.insertMany([{name: "mumbai",state:"MH"}, {name: "pune", state:"MH"}])  
    14. {  
    15.         "acknowledged" : true,  
    16.         "insertedIds" : [  
    17.                 ObjectId("5e1b0ea24fd72f15bdc222c9"),  
    18.                 ObjectId("5e1b0ea24fd72f15bdc222ca")  
    19.         ]  
    20. }  
    21. MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.City.find({})  
    22. "_id" : ObjectId("5e1b0ea24fd72f15bdc222c9"), "name" : "mumbai""state" : "MH" }  
    23. "_id" : ObjectId("5e1b0ea24fd72f15bdc222ca"), "name" : "pune""state" : "MH" }  
    24. MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.City.find({"name":"mumbai"})  
    25. "_id" : ObjectId("5e1b0ea24fd72f15bdc222c9"), "name" : "mumbai""state" : "MH" }  
    26. MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.City.find({}).limit(1)  
    27. "_id" : ObjectId("5e1b0ea24fd72f15bdc222c9"), "name" : "mumbai""state" : "MH" }  
    28. MongoDB Enterprise Cluster0-shard-0:PRIMARY> db.City.findOne({})  
    29. {  
    30.         "_id" : ObjectId("5e1b0ea24fd72f15bdc222c9"),  
    31.         "name" : "mumbai",  
    32.         "state" : "MH"  
    33. }  
We can also delete or remove the databases, collections and also documents. Also we can update the documents.
  1. db.collection.remove()  
  2. db.users.remove({})  
Conditional remove:
  1. db.users.remove( { status : "P" } )  
Open Mongo Atlas and select Cluster and then click Collection. You will get all new databases, collections and documents.
 
MongoDB Atlas 
 

Advantages of MongoDB Atlas

  • No download and installation required
  • Automatic updation available
  • Monitoring tools available with alerts
  • Inbuilt security and scalability available
  • Good performance
Here we are concluding MongoDB Atlas. So, we started with understanding about MongoDB and setup, and also started with basic querying. In the next article we will try to access MongoDB with other programming languages like C#, Javascript, or Python and perform CRUD operations.


Similar Articles