I got result after aggregate like
{data: [{"_id":"61922aed85c74b2d1ef671bb","employee":{"firstName":"fname","lastName":"lname","middleName":"mname"},"days":[{"_id":"61922aed85c74b2d1ef671be","day":"2021-09-01T21:00:00.000Z","data":{"hours":0,"type":"OT","_id":"61922aed85c74b2d1ef671bf","updateDate":"2021-11-15T09:39:57.624Z"}},{"_id":"61922aed85c74b2d1ef671c0","day":"2021-09-02T21:00:00.000Z","data":{"hours":0,"type":"OT","_id":"61922aed85c74b2d1ef671c1","updateDate":"2021-11-15T09:39:57.625Z"}}]}]}
Is it possible extract inner object of data of days array to the parent level.
From this
{"_id":"61922aed85c74b2d1ef671be","day":"2021-09-01T21:00:00.000Z","data":{"hours":0,"type":"OT","_id":"61922aed85c74b2d1ef671bf","updateDate":"2021-11-15T09:39:57.624Z"}}
to this
{"_id":"61922aed85c74b2d1ef671be","day":"2021-09-01T21:00:00.000Z", "hours":0,"type":"OT", "updateDate":"2021-11-15T09:39:57.624Z"}
CodePudding user response:
Simply use $project
like this:
db.collection.aggregate([
{
"$project": {
"day": 1,
"hours": "$data.hours",
"type": "$data.type",
"updateDate": "$data.updateDate"
}
}
])
Example here
Or to update values inside the array:
db.collection.aggregate([
{
"$project": {
"_id": 1,
"employee": 1,
"days": {
"$map": {
"input": "$days",
"as": "d",
"in": {
"_id": "$$d._id",
"day": "$$d.day",
"hours": "$$d.data.hours",
"type": "$$d.data.type",
"updateData": "$$d.data.updateDate"
}
}
}
}
}
])
Example here