I have this document structure:
_id: "xx"
statuses: [{
status: "pending",
timestamp: "1 january 10 pm",
}, {
status: "accepted",
timestamp: "2 january 2 am",
}]
now i would like to add the timestamp of accepted in the root structure.
_id: "xx"
statuses: [{
status: "pending",
timestamp: "1 january 10 pm",
}, {
status: "accepted",
timestamp: "2 january 2 am",
}]
last_accepted_at: "2 january 2 am"
I know how to set value based on another root field, but not from an item of array that I have to filter.
I tried:
db.task.findOneAndUpdate( { last_accepted_at: { $exists: false } }, { $set: { "last_accepted_at": "statuses.$[element].timestamp" }}, { arrayFilters: [ { "element.status": "accepted" } ] }, )
it has error
uncaught exception: Error: findAndModifyFailed failed: {
"ok" : 0,
"errmsg" : "The array filter for identifier 'element' was not used in the update { $set: { last_accepted_at: \"statuses.$[element].timestamp\" } }",
"code" : 9,
"codeName" : "FailedToParse"
} :
I also tried:
db.task.findOneAndUpdate( { last_accepted_at: { $exists: false } }, { $set: { "last_accepted_at": "statuses.$[element].timestamp" }}, { arrayFilters: [ { "element.$.status": "accepted" } ] }, )
uncaught exception: Error: findAndModifyFailed failed: {
"ok" : 0,
"errmsg" : "The array filter for identifier 'element' was not used in the update { $set: { last_accepted_at: \"statuses.$[element].timestamp\" } }",
"code" : 9,
"codeName" : "FailedToParse"
} :
How to achieve this? also I would like to use updateMany since its a migration file to update all old data. Thanks