0% found this document useful (0 votes)
39 views6 pages

MongoDB CRUD Operations Overview

CRUD operations in MongoDB encompass Create, Read, Update, and Delete actions performed on documents within a schema-less, document-oriented database. MongoDB supports flexible data structures, horizontal scalability through sharding and replication, and utilizes a JavaScript-based query language. It is ideal for applications requiring real-time analytics, content management, and rapid development with varied schema requirements.

Uploaded by

xdnik76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views6 pages

MongoDB CRUD Operations Overview

CRUD operations in MongoDB encompass Create, Read, Update, and Delete actions performed on documents within a schema-less, document-oriented database. MongoDB supports flexible data structures, horizontal scalability through sharding and replication, and utilizes a JavaScript-based query language. It is ideal for applications requiring real-time analytics, content management, and rapid development with varied schema requirements.

Uploaded by

xdnik76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

 Explain basic operations CRUD in MongoDB.

CRUD operations in MongoDB refer to the basic operations you can perform on documents in a
MongoDB database.

MongoDB (NoSQL) - Key Features

1. Schema-less:
MongoDB is a schema-less database, meaning you can store heterogeneous data in the same
collection without a predefined schema. Each document can have its own structure.
Example:

 { "name": "Alice", "age": 30 }

 { "name": "Bob", "email": "bob@[Link]" }

2. Document-Oriented:
MongoDB stores data in flexible, JSON-like documents. These documents can contain nested
sub-documents and arrays, allowing for a rich data structure.
Example:

{ "name": "John", "address": { "city": "New York", "zip": "10001" },"hobbies": ["reading",
"gaming"] }

3. Scalability:
MongoDB is built for horizontal scalability through:

 Sharding: Splits data across multiple servers.

 Replication: Maintains multiple copies of data for high availability and fault tolerance.
It is ideal for large-scale applications with high throughput needs.

4. Query Language:
MongoDB uses a query language based on JavaScript, with JSON-like syntax and powerful
operators.
Example: [Link]({ age: { $gt: 25 } });

5. Use Cases:
MongoDB is well-suited for:

 Real-time analytics
 Content management systems
 Mobile and web applications
 Projects requiring flexible schemas and rapid development

Mongosh show dbs


// Syntax to create a collection: [Link](name, options);\

// Example: [Link]("users");

1. Create (C)
To create a new document or insert data into a collection:

[Link]({ key: value });

// Example: [Link]({ name: "Alice", age: 30, email: "alice@[Link]" });

You can also insert multiple documents at once using insertMany():

[Link]([

{ key1: value1 },

{ key2: value2 },

// More documents...

]);

// Example:

[Link]([

{ name: "Bob", age: 25, email: "bob@[Link]" },

{ name: "Charlie", age: 35, email: "charlie@[Link]" }

]);

2. Read (R)
To retrieve or read documents from a collection:

// Find all documents in a collection [Link]();

// Find documents that match a specific condition [Link]({ condition });

// Example: [Link](); // Find all documents in the 'users' collection

[Link]({ age: { $gt: 25 } }); // Find users where age is greater than 25

3. Update (U)
To update existing documents in a collection:

// Update a single document that matches a condition

[Link]({ filter }, { $set: { update } });

// Update multiple documents that match a condition

[Link]({ filter }, { $set: { update } });

// Example: [Link]({ name: "Alice" }, { $set: { age: 31 } }); // Update Alice's age

to 31

[Link]({ age: { $lt: 30 } }, { $inc: { age: 1 } }); // Increment age by

1 for all users under 30

4. Delete (D)
To delete documents from a collection:

// Delete a single document that matches a condition

[Link]({ filter });

// Delete all documents that match a condition

[Link]({ filter });

// Example:

[Link]({ name: "Bob" }); // Delete the document where name is Bob

[Link]({ age: { $gte: 40 } }); // Delete all users who are 40 years or

older

You might also like