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

MONGODB Experiment 1

The document outlines various MongoDB operations including creating a database and collection, inserting, querying, updating, and deleting documents. It also demonstrates the use of projection to include or exclude specific fields and illustrates the application of WHERE clauses, AND, and OR operations in queries. Examples are provided for each operation using a 'students' collection in the 'myStudentDatabase' database.

Uploaded by

Bhavya Sri G
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)
12 views3 pages

MONGODB Experiment 1

The document outlines various MongoDB operations including creating a database and collection, inserting, querying, updating, and deleting documents. It also demonstrates the use of projection to include or exclude specific fields and illustrates the application of WHERE clauses, AND, and OR operations in queries. Examples are provided for each operation using a 'students' collection in the 'myStudentDatabase' database.

Uploaded by

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

Experiment 1:

a) Execute the Commands of MongoDB and operations in MongoDB : Insert, Query, Update,
Delete and Projection. (Note: use any collection)
Create Database:
use myStudentDatabase
Create Collection in the Database:
db.createCollection("students")
Insert:
Insert One Document:
db.students.insertOne({ name: "Amy", age: 20, department: "AIML" })
db.students.insertOne({ _id: 1, name: "Arya", age: 22, department: "CSE" })
Insert Multiple Documents:
db.students.insertMany([
{ _id: 2, name: "Chirag", age: 24, department: "ECE" },
{ _id: 3, name: "Sai", age: 23, department: "AIML" },
{ _id: 4, name: "Shiva", age: 22, department: "CSE" }
])
Query:
db.students.find()
Update:
Update One Document:
db.students.updateOne(
{ _id: 2 },
{ $set: { age: 25 } }
)
Update Multiple Documents:
db.students.updateMany(
{ department: "ECE" },
{ $set: { age: 23 } }
)
Delete:
Delete One Document:
db.students.deleteOne({ _id: 3 })
Delete Multiple Documents:
db.students.deleteMany({ department: "ECE" })
Projection:
Project Specific Fields (Include Only name and department):
db.students.find({}, { name: 1, department: 1, _id: 0 })
Exclude a Field (Exclude age):
db.students.find({}, { age: 0 })
Projection with Query (Find students from CSE but only return name and age)
db.students.find({ department: "CSE" }, { name: 1, age: 1, _id: 0 })

b) Illustration of Where Clause, AND,OR operations in MongoDB.


WHERE Clause (Filtering Documents)
db.students.find({ department: "CSE" })
AND Operation ($and)
Find students who belong to CSE and have age 22.
db.students.find({
$and: [
{ department: "CSE" },
{ age: 22 }
]
})
Equivalent shorthand (MongoDB implicitly applies $and if multiple fields are specified in the
query):
db.students.find({ department: "CSE", age: 22 })
OR Operation ($or)
Find students who are either from CSE or have age 24.
db.students.find({
$or: [
{ department: "CSE" },
{ age: 24 }
]
})
Combining AND and OR
Find students who are from CSE and either age 22 or 23.
db.students.find({
$and: [
{ department: "CSE" },
{$or: [{ age: 22 }, { age: 23 }] }
]
})

You might also like