0% found this document useful (0 votes)
0 views

MongoDB_Query_Language_Detailed_Notes

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)
0 views

MongoDB_Query_Language_Detailed_Notes

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
You are on page 1/ 3

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:

db.createCollection("students")

- Insert a Single Document:

db.students.insertOne({ name: "Arun", age: 21, department: "CSE" })

- Insert Multiple Documents:

db.students.insertMany([

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

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

])

3. Read Operations

- Fetch All Documents:

db.students.find()
MongoDB Query Language - Detailed Notes

- Filter Documents:

db.students.find({ department: "CSE" })

- Project Specific Fields:

db.students.find({ department: "CSE" }, { name: 1, _id: 0 })

- Comparison Operators:

db.students.find({ age: { $gt: 21 } }) // Greater than 21

db.students.find({ age: { $lte: 22 } }) // Less than or equal to 22

- Logical Operators:

db.students.find({ $or: [ { age: 21 }, { department: "IT" } ] })

4. Update Operations

- Update a Single Document:

db.students.updateOne({ name: "Arun" }, { $set: { age: 22 } })

- Update Multiple Documents:

db.students.updateMany({ department: "CSE" }, { $set: { status: "active" } })

5. Delete Operations
MongoDB Query Language - Detailed Notes

- Delete One Document:

db.students.deleteOne({ name: "Banu" })

- Delete Multiple Documents:

db.students.deleteMany({ department: "IT" })

6. Sorting and Limiting

- Sort by Age Ascending:

db.students.find().sort({ age: 1 })

- Sort by Name Descending:

db.students.find().sort({ name: -1 })

- Limit Results:

db.students.find().limit(2)

7. Aggregation Example

- Group by Department and Count Students:

db.students.aggregate([

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

])

You might also like