student
collection look like below.
[{
"_id" : NumberLong(1),
"studentId" : "70133",
"subjects" : [
"Mathematics",
"Biology and Zoology"
]
},
{
"_id" : NumberLong(2),
"studentId" : "70134",
"subjects" : [
"Mathematics",
"English"
]
},
{
"_id" : NumberLong(3),
"studentId" : "70135",
"subjects" : [
"English",
"Mathematics",
"Chemistry"
]
}]);
I want to write a query which will update the student collection subject array values like this:
- If subject matches
English
have to be updated to ENG - If subject matches
Biology
andZoology
have to be updated to BAZ - If subject matches
Mathematics
have to be updated to MAT - If subject matches
Chemistry
have to be updated to CHM
After update documents should look like below. How Can I achieve this.
[{
"_id" : NumberLong(1),
"studentId" : "70133",
"subjects" : [
"MAT",
"BAZ"
]
},
{
"_id" : NumberLong(2),
"studentId" : "70134",
"subjects" : [
"MAT",
"ENG"
]
},
{
"_id" : NumberLong(3),
"studentId" : "70135",
"subjects" : [
"ENG",
"MAT",
"CHM"
]
}]);