1

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'
            }
        }},
])
1
  • 2
    look at this answer, you just need a $group stage like this answer. Commented Jun 23, 2021 at 3:53

1 Answer 1

1

In the _id field of the group, you should specify only the keys you want to be grouped by (city in your case).

Followed by that key, the rest of the keys are additional keys you want from the query result. $addToSet will append each finding of the grouped field to an array without duplicates.

Below is the Aggregation code you are looking for:

cursor = db.aggregate([
  {
    "$group": {
      "_id": "$city",
      "value": {
        "$addToSet": "$value"
      }
    }
  },
])

In the about code, _id consists of grouped city names.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.