logo-StatusNeo
d

WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

Image Alt

StatusNeo

MongoDB Part-1 (Introduction)

What is MongoDB?

  • MongoDB is a NO-SQL database.
  • Managed by the company that is also known as MongoDB.
  • The name mongo comes from the word humongous because we can store and analyze a huge amount of datasets.
  • In MongoDB, we have databases inside that databases we have collections, i.e. similar to tables that we have in SQL.
  • In each collection we have a set of documents, documents are the JSON, objects, or dictionary whatever you want to call them. Eg-> {“name” : “Utkarsh”}
  • MongoDB is schemaless, i.e. no two documents are required to have the same fields. That’s what makes it a Non-Relational Database.
  • JSON object consists of key-value pairs, where the key will be of type string and value can be string number or a nested object.
  • By having nested objects we can have many sub-documents inside a single document.
  • {‘name’ : ‘Utkarsh’, ‘address’ : { ‘city’ : ‘Kanpur’, ‘post’ : { ‘number’ : 208001 }}} here you can see in one document we are having many documents.
  • By this nested functionality we can avoid the complex queries of SQL like joins and all, and can directly fetch the result.
  • In the backend the JSON data is stored as BSON data, BSON means Binary JavaScript Object Notation.
  • BSON helps in reducing the size of data and increasing the speed of data transactions.

Characteristics Of MongoDB

  • MongoDB is very flexible in terms of data storage since it is schemaless so we need to worry about the structure to be followed.
  • MongoDB supports Indexing making it very fast for the read operations.
  • MongoDB follows master-slave relation that helps in data security and replication.
  • GeoSpatial queries are supported in MongoDB.
  • Load Balancing is done with the help of shards(sharding).
  • Horizontal scaling is also supported.
  • Map Reducers and Aggregation queries are supported.
  • Regex searches and ad-hoc queries are very easy to use.

Installation and Connection

  • We can directly download MongoDB from their official website and use their community version or you can also go for paid versions.
  • For this blog, I have used the MongoDB cloud to host my database there only.
  • We will be using the free cluster that is provided by MongoDB.
  • Once you create your cluster it will look something like this.
  • For practice purposes, you can use the dummy dataset provided by MongoDB itself.
  • Once you add that dataset then your database will look something like this.
  • In the sample dataset, we are going to have around 8 databases with 21 collections that are a very good dataset for testing and practicing purposes.
  • Now for the hands-on and querying purposes, we are going to use Robo3T for the visualization and analysis of our dataset.
  • It can be directly downloaded from their official website, once downloaded we can put our mongo connection string there and connect to it.
  • After connection, it will look something like this.
  • Now we are all done with the connections and installations so let’s get our hands dirty.

Basic Commands use in MongoDB

  • To check the databases – show dbs
  • To check the collections- show collections
  • To use a particular database- use db_name
  • By default admin, config and local databases are present.
  • For creating a database you are not required to use any command you can just type use <your_db_name>, it will create a database for you.
  • Similary for the collections you can directly write db.collectionName.insertOne({‘key’ : value})
  • Else you can use db.createCollection(collectionName, {capped : true, size :3071400, max :1000})
  • The capped keyword is used when we want to restrict the document from the updates when the updates result in a size greater than the original one.
  • Max keyword is used to allow the maximum number of documents in a collection.
  • for dropping a collection db.collectionName.drop()
  • for dropping the database you can go inside that by using dbName and then run db.dropDatabase()




  • for finding or searching a document in a collection we directly write db.collection.find({}), for the specific value we can pass the key-value pair inside the find.

Conclusion

  • In this part, we have covered the basics of MongoDB.
  • In the upcoming parts, we are going to cover the CRUD operations as well as aggregation and complex queries.

References-

An optimistic Engineer passionate about technology, science, astrology, and photography. Visit this to know more- https://utkarshshukla.herokuapp.com/

Add Comment