0

Hi i have some question what i want to achieve is to sort progress in index 1 how can i do it with mongodb? i try some code but cannot achieve it

{ "fb_id" : "user1", "progress" : [  1, 20 ] }
{ "fb_id" : "user2", "progress" : [  2, 10 ] }
{ "fb_id" : "user3", "progress" : [  3 ] }

the result i expected is some kind like this

{ "fb_id" : "user2", "progress" : [  10 ] }
{ "fb_id" : "user1", "progress" : [  20 ] }

It is that i can achieve it through some basic mongodb query? Thank you

IWantMore
  • 13
  • 3

1 Answers1

0

Probably more complete:

db.collection.find(
    { "progress.1": {$exists: true} },
    { _id:0, fb_id: 1, progress: {$slice: [-1, 1]}}
).sort({"progress.1": 1})

So, matching, projecting and sorting to your desired result. The key is in the positional notation.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Thanks a lot i messed up when try to choose the index with "progress[1]". i got it now how to choose array index from your solution. Thank you – IWantMore Feb 20 '14 at 07:07