December 23, 2024

What is Database?

  • Data is a collection of small unit of information which carries some useful meaning and which is essential for future purposes.
  • So it is necessary to store this information to some secure place so that it can accessed for future purposes easily whenever required.
  • Database is a organized collection of data so that it can be easily accessed and managed.

Types of Database

What is NoSQL Database?

  • NoSQL database stands for “Not Only SQL” or “Not SQL.” Though a better term would be “NoREL”, NoSQL caught on. Carl Strozz introduced the NoSQL concept in 1998.
  • NoSQL Database is a non-relational Data Management System, that does not require a fixed schema. It avoids the use of complex operations like joins and it is easy to scale.
  • The major purpose of using a NoSQL database is for distributed data stores with humongous data storage needs.
  • NoSQL is used for Big data and real-time web apps. For example, companies like Twitter, Facebook and Google collect terabytes of user data every single day.

Types of NoSQL Database

There are Four big NoSQL database types

  1. Key value store
  2. Document Store
  3. Column oriented Database
  4. Graph Database

Introduction to MongoDB

  1. Document oriented database (Uses JSON)
  2. Schema free
  3. Performant
  • Written in C++
  • Full Index Support
  • No transactions(has atomic operations)
  • Memory mapped files(delayed writes)

4. Scalable

  • Replication
  • Auto sharding

5. Commercially Supported

What is collection?

  • Collection is a group of MongoDB documents.
  • It is the equivalent of an RDBMS table.
  • A collection exists within a single database.
  • Collections do not enforce a schema.
  • Documents within a collection can have different fields.
  • Typically, all documents in a collection are of similar or related purpose.

What is Document in MongoDB?

  • A document is a set of key-value pairs.
  • Documents have dynamic schema.
  • Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure.
  • Common fields in a collection’s documents may hold different types of data.

Advantages of MongoDB

  • Schema less
  • Structure of a single object is clear.
  • No complex joins.
  • Deep query-ability.
  • Ease of scale-out − MongoDB is easy to scale.
  • Conversion/mapping of application objects to database objects not needed.
  • Uses internal memory for storing the (windowed) working set, enabling faster access of data.

Why use MongoDB?

  • Document Oriented Storage − Data is stored in the form of JSON style documents.
  • Index on any attribute
  • Replication and high availability
  • Auto-Sharding
  • Rich queries
  • Fast in-place updates
  • Professional support by MongoDB

Applications of MongoDB

  • Big Data
  • Content Management and Delivery
  • Mobile and Social Infrastructure
  • User Data Management
  • Data Hub

Replica sets in MongoDB

  • Replication is the process of synchronizing data across multiple servers.
  • Replication provides redundancy and increases data availability with multiple copies of data on different database servers.
  • Replication protects a database from the loss of a single server.
  • Replication also allows you to recover from hardware failure and service interruptions.
  • With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.

Sharding in MongoDB

  • Sharding is the process of storing data records across multiple machines and it is MongoDB’s approach to meeting the demands of data growth.
  • As the size of the data increases, a single machine may not be sufficient to store the data nor provide an acceptable read and write throughput.
  • Sharding solves the problem with horizontal scaling.
  • With sharding, you add more machines to support data growth and the demands of read and write operations.

Environment setup

There are 2 ways of using mongoDB

  1. Using MongoDB Atlas(Database as a service offered by mongoDB)
  2. Using MongoDB locally in our System

For using mongoDB locally

  • Download mongoDB community server from its official website.
  • After installation set the path of mongodb bin folder in your System environment variables.
  • To start the mongodb server locally type mongod.exe in powershell
  • To start the interactive shell interface for working with mongodb type mongo.exe (Now in powershell, you can run various commands to interact with mongoDB)

MongoDB compass

  • MongoDB compass is a GUI software offered by mongoDB for interacting with our Database easily. It can be used for running mongoDB locally and also with MongoDB Atlas.

Database Commands

  • MongoDB use DATABASE_NAME is used to create database. The command will create a new database if it doesn’t exist, otherwise it will return the existing database.
  • If you want to check your databases list, use the command show dbs.
  • To check your currently selected database, use the command db.
  • MongoDB db.dropDatabase() command is used to drop a existing database.

Collection Commands

  • MongoDB db.createCollection(name, options) is used to create collection.
  • You can check the created collection by using the command show collections.

Creating Documents

  • To insert single record of data into your MongoDB collection, you can use insert() or insertOne() commands
  • To insert array of records you can use insertMany command.

Finding Documents

  • In order to get all documents in mongodb collection, use command db.COLLECTION_NAME.find()
  • In order to find only one document, use command db.COLLECTION_NAME.findOne()

Updating Documents

  • The “db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPPDATED_DATA)” method updates the values in the existing document.

Deleting Document

MongoDB remove() method is used to remove a document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag.

  • deletion criteria − (Optional) deletion criteria according to documents will be removed.
  • justOne − (Optional) if set to true or 1, then remove only one document.

MongoDB compass

MongoDB Compass is a powerful GUI for querying, aggregating, and analyzing your MongoDB data in a visual environment. Compass is free to use and source available, and can be run on macOS, Windows, and Linux.

Conclusion

I hope you found this post useful and you have learnt what is MongoDB and how to use MongoDB. For more information about MongoDB visit this URL. Thank you for reading, have a nice day 😊

Leave a Reply

Your email address will not be published. Required fields are marked *