'changing format using pymongo
This is the MongoDb query which i was using in pymongo to get the value
query = [{"$match": {"scheduledAt": {"$gte": startTime, "$lt": endTime}}},
{"$project": {"_id": "$type","countss": {"$cond": {"if": {"match$isArray": "$inputUrls"},"then": {"$size": "$inputUrls"},"else": "NA"}}}},
{"$group": {"_id":"$_id","total_count":{"$sum": "$countss"}}}]
Output which i was getting
{_id: 'a', total_count : 3}
{_id: 'b', total_count : 2}
required Output
{'a' : 3}
{'b' : 2}
Solution 1:[1]
You need $replaceRoot stage as last stage with $arrayToObject to convert document into key value pair.
{
"$replaceRoot": {
"newRoot": {
"$arrayToObject": [
[
{
k: "$_id",
v: "$total_count"
}
]
]
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yong Shun |
