Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • hi, thanks for your answer, but your solution doesn't do this for each value from an array I get a full document. how can I get it? UPD: I want to get 6 (six) results in the first and second cases Commented Sep 25, 2015 at 12:37
  • 1
    @d-d Of course it does. Sorting by "index" ( your question title, despite the multiple questions - and please don't do that again, first post forgiven ) does not affect the document in any way. As for the .aggregate() approach, when using operators like $group or $project it it up to "you" to define the fields you want to return. Operators like $first will help you here. Read some documentation and don't make hasty comments like "does not work" to those with more knowledge than yourself when you want help. Commented Sep 25, 2015 at 12:42
  • @d-d And for each value in an array ( I can shout too ) then just use $unwind without the $group and sort on the value. Pretty simple if you spent more than two seconds actually reading the useful information you were just given. Commented Sep 25, 2015 at 12:44
  • I've just checked again, and your solution returns only two results, not six. Commented Sep 25, 2015 at 12:55
  • but thanks for pointing to $unwind, it seems to be what I'm looking for Commented Sep 25, 2015 at 12:57