node.jsmongodb

How do I sort new items by newest?


I have this nodeJS code but whenever I post my data, the newest data is added on the end instead of start. I tried adding Crud.find().sort('_id', -1).exec((err, data) => but it still doesn't work.

app.get('/v2/posts', (req,res) => {
    Crud.find((err, data)=> {
        if (err) {
            res.status(500).send(err)
        } else {
            res.status(200).send(data)
        }
    })
});

Solution

  • try-

    Crud.find().sort({$natural:-1})