2

I have docs like:

{
    "products": [
        {
           "uid": "aside-11kka-asdm12-asdjl"
           "price": 123,
           "count": 123
        }
    ]
}

And I want to group this into array of products, using aggregation (product uids can be repeated in another object)

1 Answer 1

3

You need to $unwind first in order to group nested array

db.collection.aggregate([
  {
    "$unwind": "$products"
  },
  {
    $group: {
      "_id": "$products.uid",
      "count": {
        $push: "$products.count"
      },
      "price": {
        $push: "$products.price"
      }
    }
  }
])
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.