Search This Blog

2024/04/19

MongoDb:Update elements of Object array

Lets consider a Cloud Training Institutes has branches across Maharashtra
in Pune & Mumbai.They conduct a common course in that constitutes AWS,Google
Cloud,Microsoft Azure and their data is saved in mongoDb in following format.

lets drop collection if exist

db.locationWiseStundentInInstitute.drop()

Now lets insert new documents into collection.

db.locationWiseStundentInInstitute.insertMany(
[
{
_id: 1,
location: "Mumbai",
students: [
{
name: "arjun",
age: 5,
email: "arjun.sawant@example.com",
fees: 15000,
scholarship: 2000,
paid: 0,
subject: [
"AWS",
"Google Cloud",
"Microsoft Azure",
],
marks: [75, 97, 49],
},
{
name: "sita",
age: 30,
email: "sita@example.com",
fees: 15000,
scholarship: 1000,
paid: 2000,
subject: [
"AWS",
"Google Cloud",
"Microsoft Azure",
],
marks: [70, 70, 89],
},
],
},
{
_id: 2,
location: "Pune",
students: [
{
name: "anil",
age: 5,
email: "anil@example.com",
fees: 15000,
scholarship: 3000,
paid: 1000,
subject: [
"AWS",
"Google Cloud",
"Microsoft Azure",
],
marks: [55, 47, 89],
},
{
name: "arjun",
age: 5,
email: "arjun.patil@example.com",
fees: 15000,
scholarship: 2000,
paid: 0,
subject: [
"AWS",
"Google Cloud",
"Microsoft Azure",
],
marks: [85, 67, 89],
},
{
name: "sunil",
age: 30,
email: "sunil@example.com",
fees: 15000,
scholarship: 1000,
paid: 0,
subject: [
"AWS",
"Google Cloud",
"Microsoft Azure",
],
marks: [40, 90, 59],
},
],
},
]
);
Condition 1:
Suppose a student arjun having email id 'arjun.sawant@example.com ,
studying in location mumbai has paid 3000 ruppes,now we need to
update his entry.

db.locationWiseStundentInInstitute.updateOne(
{ "students.email": "arjun.sawant@example.com",location: "Mumbai" },
{ $inc: { "students.$.paid": 3000 } }
);
This will update only paid value for arjun in Mumbai location &
not for arjun in pune location.


Suppose a student arjun having email id arjun.sawant@example.com,
studying in location mumbai asked for changing email id to
arjun@gmail.com then

//update existing property in matched object array element
db.locationWiseStundentInInstitute.updateOne(
{ "students.email": "arjun.sawant@example.com",location: "Mumbai" },
{ $set: { "students.$.email": "arjun@gmail.com" } }
);
This will update only email for arjun in Mumbai location & not
for arjun in pune location.

Condition 2:
Suppose a student sita having email id sita@example.com, studying in
location mumbai has given his co-student free meal pass.

//add freeMealPass to all elements of array student as they are
// co-students
db.locationWiseStundentInInstitute.updateOne(
{ "students.email": "sita@example.com",location:"Mumbai" },
{ $set: { "students.$[].freeMealPass": true } }
);

but it will not update students array for other location.

Condition 3:
Suppose sunil from Pune with email id sunil@example.com has earned
"Creative" badge.

db.locationWiseStundentInInstitute.updateOne(
{ "students.email": "sunil@example.com",location:"Pune" },
{ $set: { "students.$.badge": ["Creative"] } }
);
Only Object of sunil from pune in student object array will be modified

Condition 4:
Suppose Institute decided to give access to all student in
location mumbai & pune to their E-Learning Portal.

db.locationWiseStundentInInstitute.updateMany(
{ location: { $in: ["Mumbai", "Pune"] } },
{ $set: { "students.$[].Elearning": "Enabled" } }
);

Final look at Collection:
Being lengthy collection I purposefully not included the output of query,
but here is final Output.

db.locationWiseStundentInInstitute.find({}).pretty();

Output:
[
{
_id: 1,
location: 'Mumbai',
students: [
{
name: 'arjun',
age: 5,
email: 'arjun.sawant@example.com',
fees: 15000,
scholarship: 2000,
paid: 3000,
subject: [ 'AWS', 'Google Cloud', 'Microsoft Azure' ],
marks: [ 75, 97, 49 ],
freeMealPass: true,
Elearning: 'Enabled'
},
{
name: 'sita',
age: 30,
email: 'sita@example.com',
fees: 15000,
scholarship: 1000,
paid: 2000,
subject: [ 'AWS', 'Google Cloud', 'Microsoft Azure' ],
marks: [ 70, 70, 89 ],
freeMealPass: true,
Elearning: 'Enabled'
}
]
},
{
_id: 2,
location: 'Pune',
students: [
{
name: 'anil',
age: 5,
email: 'anil@example.com',
fees: 15000,
scholarship: 3000,
paid: 1000,
subject: [ 'AWS', 'Google Cloud', 'Microsoft Azure' ],
marks: [ 55, 47, 89 ],
Elearning: 'Enabled'
},
{
name: 'arjun',
age: 5,
email: 'arjun.patil@example.com',
fees: 15000,
scholarship: 2000,
paid: 0,
subject: [ 'AWS', 'Google Cloud', 'Microsoft Azure' ],
marks: [ 85, 67, 89 ],
Elearning: 'Enabled'
},
{
name: 'sunil',
age: 30,
email: 'sunil@example.com',
fees: 15000,
scholarship: 1000,
paid: 0,
subject: [ 'AWS', 'Google Cloud', 'Microsoft Azure' ],
marks: [ 40, 90, 59 ],
badge: [ 'Creative' ],
Elearning: 'Enabled'
}
]
}
]

No comments:

Post a Comment