1

I am doing an aggregate and on some stage i have following results:

[{ "_id" : 1, "array_field": [1, 2, 3]},
 { "_id" : 2, "array_field": [3, 2, 1]}]

I want to get the array_field sorted on next stage:

[{ "_id" : 1, "array_field": [1, 2, 3]},
 { "_id" : 2, "array_field": [1, 2, 3]}]

What is the possibilities? The $sort didnt helped me.

0

1 Answer 1

2

You need to $unwind the array_field and then again $group to rollback

db.collection.aggregate([
  { "$unwind": "$array_field" },
  { "$sort": {
    "array_field": 1
  }},
  { "$group": {
    "_id": "$_id",
    "array_field": {
      "$push": "$array_field"
    }
  }}
])
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.