0% found this document useful (0 votes)
15 views3 pages

MongoDB Query Language Essentials

This document provides detailed notes on MongoDB Query Language, covering basic commands, create, read, update, and delete operations. It includes examples for inserting, fetching, filtering, updating, and deleting documents, as well as sorting, limiting results, and performing aggregation. The notes serve as a comprehensive guide for using MongoDB effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

MongoDB Query Language Essentials

This document provides detailed notes on MongoDB Query Language, covering basic commands, create, read, update, and delete operations. It includes examples for inserting, fetching, filtering, updating, and deleting documents, as well as sorting, limiting results, and performing aggregation. The notes serve as a comprehensive guide for using MongoDB effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MongoDB Query Language - Detailed Notes

1. Basic Commands

- show dbs : List all databases.

- use <db_name> : Switch to a database (e.g., use studentDB).

- show collections : List all collections in the current database.

2. Create Operations

- Create a Collection:

[Link]("students")

- Insert a Single Document:

[Link]({ name: "Arun", age: 21, department: "CSE" })

- Insert Multiple Documents:

[Link]([

{ name: "Banu", age: 22, department: "ECE" },

{ name: "Chitra", age: 23, department: "IT" }

])

3. Read Operations

- Fetch All Documents:

[Link]()
MongoDB Query Language - Detailed Notes

- Filter Documents:

[Link]({ department: "CSE" })

- Project Specific Fields:

[Link]({ department: "CSE" }, { name: 1, _id: 0 })

- Comparison Operators:

[Link]({ age: { $gt: 21 } }) // Greater than 21

[Link]({ age: { $lte: 22 } }) // Less than or equal to 22

- Logical Operators:

[Link]({ $or: [ { age: 21 }, { department: "IT" } ] })

4. Update Operations

- Update a Single Document:

[Link]({ name: "Arun" }, { $set: { age: 22 } })

- Update Multiple Documents:

[Link]({ department: "CSE" }, { $set: { status: "active" } })

5. Delete Operations
MongoDB Query Language - Detailed Notes

- Delete One Document:

[Link]({ name: "Banu" })

- Delete Multiple Documents:

[Link]({ department: "IT" })

6. Sorting and Limiting

- Sort by Age Ascending:

[Link]().sort({ age: 1 })

- Sort by Name Descending:

[Link]().sort({ name: -1 })

- Limit Results:

[Link]().limit(2)

7. Aggregation Example

- Group by Department and Count Students:

[Link]([

{ $group: { _id: "$department", count: { $sum: 1 } } }

])

Common questions

Powered by AI

The db.students.find() command with a projection differs by only displaying specified fields rather than all. Example: db.students.find({ department: 'CSE' }, { name: 1, _id: 0 }) will return only the name field without the _id field .

Use the $or logical operator to combine conditions: db.students.find({ $or: [ { age: 21 }, { department: 'IT' } ] }).

To update the age of the student named 'Arun' to 22, use the updateOne method: db.students.updateOne({ name: "Arun" }, { $set: { age: 22 } }).

To filter documents for students in the 'CSE' department, you can use the MongoDB find command with a specific filter: db.students.find({ department: "CSE" }).

To limit query results to two documents, use the limit method: db.students.find().limit(2).

Use db.students.insertMany to create multiple documents in a single operation. This is efficient for bulk inserts, saving network overhead and ensuring atomic insertion: db.students.insertMany([ { name: "Banu", age: 22, department: "ECE" }, { name: "Chitra", age: 23, department: "IT" } ]).

The aggregation framework provides tools for grouping and counting. To group by department and count students, use db.students.aggregate([ { $group: { _id: "$department", count: { $sum: 1 } } } ]).

To sort the students collection by age in ascending order, use db.students.find().sort({ age: 1 }).

To manually create a collection named 'students', use db.createCollection("students"). Manual creation may be necessary to specify options such as storage size, validation rules, or indexes. It provides control over collection properties that automatic creation during insert operations doesn’t offer .

To delete a single document, use deleteOne, which removes the first matched document: db.students.deleteOne({ name: "Banu" }). For multiple documents, use deleteMany, which removes all matched documents: db.students.deleteMany({ department: "IT" }).

You might also like