0

I am trying to fetch all documents in ASC(ascending) order. My data is

[{
  _id:xxxxxxx,
  bookdetails:{
    bookCost:{
     price:500,
     discount:10,
     createdAt:'2017-05-11',
    },
    name: yyyyyy,
   }
},{
  _id:xxxxxxx,
  bookdetails:{
    bookCost:{
     price:600,
     discount:10,
     createdAt:'2017-02-11',
    },
    name: yyyyyy,
   }
}]

Here is my code

db.Book.find({ bookId:id}, ('_id bookdetails'))
        .populate({
            path: 'bookdetails'
          })
        .sort({ 'bookCost.createdAt': 1 })
        .exec(function (err, docs) {
            if (!err) {
                let response =;
                callback(response);
            } else {
                callback({})
            }
        })

I want to display books based on Date created? Can you please correct me.

vibhor1997a
  • 2,336
  • 2
  • 17
  • 37
Kittu
  • 1,577
  • 1
  • 9
  • 12

1 Answers1

0
db.Book.find({ bookId:id}, ('_id bookdetails'))
    .populate({
        path: 'bookdetails'
      })
    .sort({ 'bookdetails.bookCost.createdAt': 1 })
    .exec(function (err, docs) {
        if (!err) {
            let response =;
            callback(response);
        } else {
            callback({})
        }
    })
Riyaz Shaikh
  • 141
  • 9