https://mongoplayground.net/p/Td1QwxiMCJ2
db.collection.update({},
[
{
"$addFields": {
"timeslot": {
"$map": {
"input": "$timeslot",
"as": "ts",
"in": {
"$cond": {
// criteria for element you want to update
"if": {
$eq: [
"$$ts.time",
"00:00"
]
},
"then": {
"$mergeObjects": [
"$$ts",
// to-be version of staff
{
staff: [
"A",
"B"
]
}
]
},
"else": "$$ts"
}
}
}
}
}
}
],
{
multi: false
})
你個schema好複雜... 要用$map 去process個timeslot array再用$cond做conditional update... api syntax嗰d唔多講了, 自己查返doc, 有d問題想問:
1. 點解date store as string?唔store as proper date object?
2. 點解timeslot 唔flatten咗佢, 成個collection一個document就一個timeslot嘅array entry?