I have a document from mongo database looking like this:
{
"_id": "00000001",
"category": "Weather",
"city": "Salt Lake City",
"date": {
"$date": {
"$numberLong": "1663236000000"
}
},
"logs": {
"2022-09-14 12:00:00": {
"temp": 55,
"humidity": 25
},
"2022-09-14 14:00:00": {
"temp": 65,
"humidity": 35
}
}
}
I am trying to query it and have it look like this:
{
"_id": "00000001",
"category": "Weather",
"city": "Salt Lake City",
"date": {
"$date": {
"$numberLong": "1663236000000"
}
},
"2022-09-14 12:00:00": "55, 25",
"2022-09-14 14:00:00": "65, 35"
}
Currently my application query looks like:
collection.aggregate(
[{
$match: {
_id: {
$exists: true
}
}
},
{
$unwind: "$logs"
},
{
$addFields: {
"series._id": "$_id",
"series.category": "$category",
"series.city": "$city",
"series.date": "$date",
}
},
{
$replaceRoot: {
newRoot: "$logs"
},
}
])
which results in:
{
"_id": "00000001",
"category": "Weather",
"city": "Salt Lake City",
"date": {
"$date": {
"$numberLong": "1663236000000"
}
},
"2022-09-14 12:00:00": {
"temp": 55,
"humidity": 25
},
"2022-09-14 14:00:00": {
"temp": 65,
"humidity": 35
}
}
My problem is that the logs will add a new field every n hours, so the field names will be dynamic. I need to set/update the values for the unwound fields from objects to a string representation. How can I set/update field values for fields generated through $unwind
aggregation like the example?
2
Answers
Building off of @nimrod serok's answer, still needed to flatten the logs field in my case. Used
mergeObjects
to flatten the field into the root document, and then usedunset
to remove the original field. This probably isn't the best way to do this but it is working for me. ThanksWhen field names are dynamic, one option is to use
$objectToArray
:See how it works on the playground example
BTW,
$unwind
is for arrays, not for objects, hence the comment by @CharchitKapoor.