Insert one record
db.createCollection("student")
{ ok: 1 }
---------------------------------------------
db.student.insertOne({
name:"prati",
age:19,
place:"pune",
department:"CE",
gpa: 3.8,
courses: ["Algorithms", "Data Structures", "Databases"])
acknowledged: true,
insertedId: ObjectId('6708257f8aecdd95464ab95e')
--------------------------------------------------------------------------------------------
insert multiple records
db.student.insertMany([
name: "Pratik",
age: 21,
department: "Computer Science",
gpa: 3.8,
courses: ["Algorithms", "Data Structures", "Databases"]
},
name: "Prathem",
age: 23,
department: "Mechanical Engineering",
gpa: 3.4,
courses: ["Thermodynamics", "Mechanics", "Design"]
},
{
name: "Sayali",
age: 22,
department: "Computer Science",
gpa: 3.9,
courses: ["Operating Systems", "Machine Learning", "Cloud Computing"]
},
name: "Soham",
age: 20,
department: "Electrical Engineering",
gpa: 3.6,
courses: ["Circuits", "Electronics", "Control Systems"]
},
name: "Sanu",
age: 21,
department: "Computer Science",
gpa: 3.7,
courses: ["Software Engineering", "Networks", "AI"]
])
acknowledged: true,
insertedIds: {
'0': ObjectId('6708b7c77ee56f41e3189116'),
'1': ObjectId('6708b7c77ee56f41e3189117'),
'2': ObjectId('6708b7c77ee56f41e3189118'),
'3': ObjectId('6708b7c77ee56f41e3189119'),
'4': ObjectId('6708b7c77ee56f41e318911a')
}
________________________________________________________________________
Q change age of priti to 20
db.student.updateOne(
name:"prati"
},
$set:{age:20}
})
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
---------------------------------------------------------------------------------------------
db.student.find().pretty()
_id: ObjectId('6708b7c77ee56f41e3189116'),
name: 'Pratik',
age: 21,
department: 'Computer Science',
gpa: 3.8,
courses: [
'Algorithms',
'Data Structures',
'Databases'
_id: ObjectId('6708b7c77ee56f41e3189116'),
name: 'prati',
age: 20,
department: 'Computer Science',
gpa: 3.8,
courses: [
'Algorithms',
'Data Structures',
'Databases'
_id: ObjectId('6708b7c77ee56f41e3189117'),
name: 'Prathem',
age: 23,
department: 'Mechanical Engineering',
gpa: 3.4,
courses: [
'Thermodynamics',
'Mechanics',
'Design'
_id: ObjectId('6708b7c77ee56f41e3189118'),
name: 'Sayali',
age: 22,
department: 'Computer Science',
gpa: 3.9,
courses: [
'Operating Systems',
'Machine Learning',
'Cloud Computing'
_id: ObjectId('6708b7c77ee56f41e3189119'),
name: 'Soham',
age: 20,
department: 'Electrical Engineering',
gpa: 3.6,
courses: [
'Circuits',
'Electronics',
'Control Systems'
_id: ObjectId('6708b7c77ee56f41e318911a'),
name: 'Sanu',
age: 21,
department: 'Computer Science',
gpa: 3.7,
courses: [
'Software Engineering',
'Networks',
'AI'
--------------------------------------------------------------------------------
Q delete record of priti
db.student.deleteOne({"name":{$eq:"prati"}})
acknowledged: true,
deletedCount: 1
db.student.find()
_id: ObjectId('6708b7c77ee56f41e3189116'),
name: 'Pratik',
age: 21,
department: 'Computer Science',
gpa: 3.8,
courses: [
'Algorithms',
'Data Structures',
'Databases'
_id: ObjectId('6708b7c77ee56f41e3189117'),
name: 'Prathem',
age: 23,
department: 'Mechanical Engineering',
gpa: 3.4,
courses: [
'Thermodynamics',
'Mechanics',
'Design'
{
_id: ObjectId('6708b7c77ee56f41e3189118'),
name: 'Sayali',
age: 22,
department: 'Computer Science',
gpa: 3.9,
courses: [
'Operating Systems',
'Machine Learning',
'Cloud Computing'
_id: ObjectId('6708b7c77ee56f41e3189119'),
name: 'Soham',
age: 20,
department: 'Electrical Engineering',
gpa: 3.6,
courses: [
'Circuits',
'Electronics',
'Control Systems'
_id: ObjectId('6708b7c77ee56f41e318911a'),
name: 'Sanu',
age: 21,
department: 'Computer Science',
gpa: 3.7,
courses: [
'Software Engineering',
'Networks',
'AI'
]
}
------------------------------------------------------------------------------------------------------------------------
Q add course "Distributed Systems" in department computer science
db.student.updateMany(
{ department: "Computer Science" },
$push: { courses: "Distributed Systems" }
},
{ upsert: true }
acknowledged: true,
insertedId: null,
matchedCount: 3,
modifiedCount: 3,
upsertedCount: 0
—-
db.student.find({department :{$eq:"Computer Science"}})
_id: ObjectId('6708b7c77ee56f41e3189116'),
name: 'Pratik',
age: 21,
department: 'Computer Science',
gpa: 3.8,
courses: [
'Algorithms',
'Data Structures',
'Databases',
'Distributed Systems'
]
_id: ObjectId('6708b7c77ee56f41e3189118'),
name: 'Sayali',
age: 22,
department: 'Computer Science',
gpa: 3.9,
courses: [
'Operating Systems',
'Machine Learning',
'Cloud Computing',
'Distributed Systems'
_id: ObjectId('6708b7c77ee56f41e318911a'),
name: 'Sanu',
age: 21,
department: 'Computer Science',
gpa: 3.7,
courses: [
'Software Engineering',
'Networks',
'AI',
'Distributed Systems'