Instead of manually manipulating data and writing it back for multiple documents, consider using the following query:
myCollection.updateMany({
}, { $set: { "days.$[].Hour.$[eachHour]": '8h' } }, {
arrayFilters: [{ 'eachHour': '08h' }]
})
Collection Data :
/* 1 */
{
"_id" : ObjectId("5bbd1396197aa5389cb7bfb7"),
"days" : [
{
"Day" : "Saturday",
"Hour" : [
"08h",
"10h"
]
},
{
"Day" : "Sunday",
"Hour" : [
"08h",
"10h"
]
}
],
"name" : "Guilherme",
"isActive" : true,
"gender" : "male"
}
/* 2 */
{
"_id" : ObjectId("5e2f085ad02e05b69487f2b3"),
"days" : [
{
"Day" : "Saturday",
"Hour" : [
"18h",
"10h"
]
},
{
"Day" : "Sunday",
"Hour" : [
"08h",
"10h"
]
}
],
"name" : "Guilherme2",
"isActive" : true,
"gender" : "male"
}
/* 3 */
{
"_id" : ObjectId("5e2f0865d02e05b69487f3a0"),
"days" : [
{
"Day" : "Saturday",
"Hour" : [
"18h",
"10h"
]
},
{
"Day" : "Sunday",
"Hour" : [
"18h",
"10h"
]
}
],
"name" : "Guilherme3",
"isActive" : true,
"gender" : "male"
}
Result :
/* 1 */
{
"_id" : ObjectId("5bbd1396197aa5389cb7bfb7"),
"days" : [
{
"Day" : "Saturday",
"Hour" : [
"8h",
"10h"
]
},
{
"Day" : "Sunday",
"Hour" : [
"8h",
"10h"
]
}
],
"name" : "Guilherme",
"isActive" : true,
"gender" : "male"
}
/* 2 */
{
"_id" : ObjectId("5e2f085ad02e05b69487f2b3"),
"days" : [
{
"Day" : "Saturday",
"Hour" : [
"18h",
"10h"
]
},
{
"Day" : "Sunday",
"Hour" : [
"8h",
"10h"
]
}
],
"name" : "Guilherme2",
"isActive" : true,
"gender" : "male"
}
/* 3 */
{
"_id" : ObjectId("5e2f0865d02e05b69487f3a0"),
"days" : [
{
"Day" : "Saturday",
"Hour" : [
"18h",
"10h"
]
},
{
"Day" : "Sunday",
"Hour" : [
"18h",
"10h"
]
}
],
"name" : "Guilherme3",
"isActive" : true,
"gender" : "male"
}
This query updates two documents in the collection.
For more information, refer to: .updateMany()-arrayFilters