0

I have an array idsToHunt - this contains x number of IDs.

I'm using this is an aggregate like:

Question.aggregate([
          {$match: {publicID: { $in: idsToHunt} },
          {$project: {_id:0, publicID:1, score:1} },
          {$sort: {score: -1}}
          ],
          function(err, found) {
            nextStep = found
            resolve();
          })

Now where I have {$sort: {score: -1}} - what i really want to do is sort by the index of idsToHunt.

So if idsToHunt = ['da22', 'ye66', '17hy']

Then I'd like the projected results to be like:

{publicID: 'da22', score: '2'}, {publicID: 'ye66', score: '2'}, {publicID: '17hy', score: '2'}

rather than a random order as it is now:

{publicID: 'da22', score: '2'}, {publicID: '17hy', score: '2'}, {publicID: 'ye66', score: '2'}

Hope that makes sense - any help would be appreciated.

Thanks.

userMod2
  • 8,312
  • 13
  • 63
  • 115
  • I see that answer - and I realise that `$in` doesn't follow a strict order however I'm still unclear how to solve? – userMod2 Sep 01 '16 at 19:57
  • The answers to the question tell you your options regarding how to solve it. There's no easy way. – JohnnyHK Sep 02 '16 at 03:51

0 Answers0