Dave
Dave

Reputation: 2018

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)
        }
    })
});

Upvotes: 0

Views: 289

Answers (1)

Mohit Gupta
Mohit Gupta

Reputation: 378

try-

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

Upvotes: 1

Related Questions