I have documents representing Users with onboarding data stored as a nested object:
{
"_id" : ObjectId("5c7eb0132e6f793bcc7f4bf7"),
"userName" : "sample_user_name",
"onBoarding" : {
"completed" : ISODate("2019-03-05T17:46:28.803Z"),
"stepId" : 8,
"started" : null
}
}
But due to a bug we are missing a date when onboarding was started, I would like to "retrieve" this information by running an update operation where "started" will be set to the same date as "completed". I have a query like:
db.getCollection('user').updateMany(
{
$and: [
{"onBoarding.started": {$exists: false}},
{"onBoarding.completed": {$exists: true}}
]},
{
$set: { "onBoarding.started": "$onBoarding.completed" }
})
This however, sets "started" to "$onBoarding" literally (as a string).
{
"_id" : ObjectId("5c7eb0132e6f793bcc7f4bf7"),
"userName" : "sample_user_name",
"onBoarding" : {
"completed" : ISODate("2019-03-05T17:46:28.803Z"),
"stepId" : 8,
"started" : "$onBoarding"
}
}
How should I write it for mongo to take a value from "onBoarding.completed" and copy this value to "onBoarding.started"?? Expected result document should look like:
{
"_id" : ObjectId("5c7eb0132e6f793bcc7f4bf7"),
"userName" : "sample_user_name",
"onBoarding" : {
"completed" : ISODate("2019-03-05T17:46:28.803Z"),
"stepId" : 8,
"started" : ISODate("2019-03-05T17:46:28.803Z")
}
}