MongoDB

Introduction to MongoDB

Introduction to CURD operations

Introduction to MongoDB

MongoDB is a popular open-source, document-oriented NoSQL database designed for storing and managing large volumes of data. It uses a flexible, schema-less design, allowing for efficient storage of JSON-like documents called BSON (Binary JSON). MongoDB is known for its scalability, performance, and ease of use, making it a preferred choice for modern web applications.

Key features of MongoDB:

  • Document-oriented storage: Stores data in flexible, JSON-like documents.
  • Schema-less: No predefined schema required, allowing for dynamic changes.
  • Scalability: Supports horizontal scaling through sharding.
  • Replication: Ensures high availability and data redundancy.
  • Indexing: Supports various types of indexes for fast query performance.
  • Aggregation: Powerful aggregation framework for data processing and analysis.

CRUD Operations in MongoDB

CRUD operations are the four basic operations performed on databases: Create, Read, Update, and Delete.

1. Create

In MongoDB, creating a document is done using the insertOne or insertMany methods to add a single or multiple documents to a collection.

// Inserting a single document

db.collection.insertOne({ name: "Alice", age: 25, city: "New York" });

// Inserting multiple documents

db.collection.insertMany([

  { name: "Bob", age: 30, city: "San Francisco" },

  { name: "Charlie", age: 35, city: "Los Angeles" }

]);

2. Read

Reading or retrieving documents from a collection is done using the find method. You can specify query criteria to filter the results.

// Finding all documents

db.collection.find({});

// Finding documents with a specific condition

db.collection.find({ age: { $gt: 30 } });

// Finding a single document

db.collection.findOne({ name: "Alice" });

3. Update

Updating documents is done using the updateOne or updateMany methods to modify one or multiple documents that match a specified filter.

// Updating a single document

db.collection.updateOne(

  { name: "Alice" },

  { $set: { age: 26 } }

);

// Updating multiple documents

db.collection.updateMany(

  { city: "San Francisco" },

  { $set: { city: "SF" } }

);

// Updating a single document
db.collection.updateOne(
  { name: "Alice" },
  { $set: { age: 26 } }
);

// Updating multiple documents
db.collection.updateMany(
  { city: "San Francisco" },
  { $set: { city: "SF" } }
);

4. Delete

Deleting documents is done using the deleteOne or deleteMany methods to remove one or multiple documents that match a specified filter.

// Deleting a single document

db.collection.deleteOne({ name: "Alice" });

// Deleting multiple documents

db.collection.deleteMany({ age: { $lt: 30 } });

MongoDB is a flexible and scalable NoSQL database that is well-suited for modern web applications. CRUD operations—Create, Read, Update, and Delete—form the basic operations for managing data within MongoDB. These operations allow developers to efficiently interact with and manipulate their data stored in MongoDB collections.

Comments

Popular posts from this blog

KTU DBMS LAB CSL 333 BTech S5 - Dr Binu V P

Procedures in PL/SQL

Cursors in PL/SQL