Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Mongo DB

User image

Published by

sanya sanya

Published at: 28th Jul, 2023
3.405 mins read

MongoDB is a popular NoSQL (non-relational) document database that is designed to store, manage, and retrieve large volumes of structured, semi-structured, and unstructured data.

It falls under the category of a document-oriented database management system.

Why MongoDB?

There are several reasons why MongoDB is chosen as a database solution for many applications and some of them are mentioned below -

  • Flexibility and Dynamic Schema - MongoDB's document-oriented nature allows for flexible and dynamic data models. It does not enforce a fixed schema, allowing you to easily add, modify, or remove fields from documents as needed.

  • Scalability and Performance - MongoDB is designed to handle large-scale data storage and high-traffic applications. It supports horizontal scaling by distributing data across multiple servers through sharding, enabling applications to scale with ease.

  • Use Case Versatility - MongoDB can be used for a wide range of use cases, including content management systems, real-time analytics, social networks, IoT applications, catalog management, and more.

MongoDB Commands

MongoDB provides a command-line interface (CLI) called the MongoDB Shell (mongo) that allows you to interact with the database and execute various commands. Here are some commonly used MongoDB commands:

  • use - Switches to the specified database or creates a new database if it doesn't exist.

  • show databases - Lists all the databases available on the MongoDB server.

  • show collections - Lists all the collections in the current database.

  • db.collectionName.find() - Retrieves documents from a collection based on the specified query criteria. The query is written in MongoDB's query language.

  • db.collectionName.insertOne() - Inserts a single document into a collection.



  • db.collectionName.insertMany() - Inserts multiple documents into a collection.

  • db.collectionName.updateOne() - This command updates the single document in a collection that matches the specified filter criteria.

  • db.collectionName.updateMany() - Updates multiple documents in a collection that match the specified filter criteria.

  • db.collectionName.deleteOne() - Deletes a single document from a collection based on the specified filter criteria.

  • db.collectionName.deleteMany() - Deletes multiple documents from a collection that match the specified filter criteria.

  • db.collectionName.aggregate() - Performs aggregation operations on a collection using a pipeline of stages, such as filtering, grouping, sorting, and more.

  • db.collectionName.createIndex() - Creates an index on one or more fields in a collection to improve query performance.

  • db.collectionName.drop() - Deletes an entire collection.

MongoDB Operators

MongoDB provides a rich set of operators that can be used in queries, updates, aggregations, and other operations. These operators allow you to perform various operations and transformations on data.

Logical Operators:

  • $and: logical AND.
  • $or: logical OR.
  • $not: Inverts the effect of a query expression.
  • $nor: logical NOR.

How to Connect MongoDB

To connect to MongoDB, you can follow these steps -

  • Install MongoDB
  • Start MongoDB Serve
  • Choose a Connection Method
  • Connect to MongoDB

Difference between SQL and NoSQL Databases

Relational databases (RDBMS) and non-relational databases (NoSQL) have significant differences in their data storage and retrieval models.

  • Data Structure -

Relational Databases - Use tables with predefined schemas to store data in rows and columns. Each row represents a record, and each column represents a field or attribute of the record.
Non-Relational Databases - Use various data models, such as key-value pairs, documents, column-family, or graphs. The data structure can be flexible and does not require a fixed schema for all records.

  • Schema -

Relational Databases - Enforce a fixed schema, meaning all records in a table must have the same set of columns and data types.
Non-Relational Databases - Offer dynamic or flexible schema, allowing different records to have different sets of fields without a fixed structure.

  • Scaling -

Relational Databases - Vertical scaling is typical, meaning increasing the server's resources (CPU, RAM) to handle more load. This approach can become expensive and has practical limitations.
Non-Relational Databases - Horizontal scaling is more common, achieved by adding more nodes to distribute the data and workload. This allows NoSQL databases to handle large-scale applications efficiently.

Library

WEB DEVELOPMENT

Basic

Frontend

Backend

Node JS

Database

Introduction to Databases

Mongo DB

Interview Questions

FAANG QUESTIONS

On this page

Why MongoDB?

MongoDB Commands

MongoDB Operators

Logical Operators:

How to Connect MongoDB

Difference between SQL and NoSQL Databases