4

I have a simple model:

user = {
  'items': [{
        'name': 'abc',
        'pages': [ObjectId("58c703a353dbaf37586b885c"), ObjectId("58c703a353dbaf37586b885d"), ..]}
   }]
};

I'm trying to sort the pages of current item:

User.findOne({'_id': id}, {'items': {$elemMatch: {'_id': id2}}})
    .populate({path: 'items.pages', select: '_id', options: { sort: { _id: -1 } }})
    .exec(function(err, user) {

    });

But I'm getting an error: Error: Cannot populate withsorton path items.pages because it is a subproperty of a document array. What should I change?

dontHaveName
  • 1,899
  • 5
  • 30
  • 54
  • 1
    It appears that this is a known, unfixed bug, according to the mongoose staff: https://github.com/Automattic/mongoose/issues/2202 – jonny Mar 01 '18 at 14:07
  • Still unfixed from what I can tell. Recommended suggestion in that GitHub issue is to use builtin [Array.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) on the returned objects. – Peter Cassetta Jul 14 '18 at 11:43

0 Answers0