I have a MongoDB collection with documents like:
{'city': 'NYC', 'value': 'blue'},
{'city': 'NYC', 'value': 'red'},
{'city': 'Boston', 'value': 'blue'},
{'city': 'Boston', 'value': 'green'}
I want to aggregate distinct values of city with a list of distinct values of value, like:
{'city': 'NYC', 'values': ['blue', 'red']},
{'city': 'Boston', 'values': ['blue', 'green']}
How can I do this in a PyMongo pipeline?
Something with a shell like:
cursor = db.aggregate([
{'$group': {
'_id': {
'value': '$value',
'city': '$city'
}
}},
])