I have a collection "logs" and I need to sort it by most recent first. With findOne() I do:
.sort($natural: -1)
However this is not doing anything now.
db('logs').find({'userId': userId}).sort({ $natural: -1}).toArray(function (err, result) {
if (err) console.error(err)
if (result.length > 0) {
console.log('Found ' + result.length + ' log entries', result)
if (callback) callback(result)
} else {
console.log('No log entries for ' + userId, userId)
if (callback) callback(null)
}
})
This outputs the same as $natural: 1. Or if I skip sort() all together.
Collection:
{_id: "5c044d13f4c6db53a047fe37", dateTime: "2018-12-02 22:22", userId: 5, event: "Failed to update Media Package sale", info: {userId: 5, AmountOfPrePaidItems: 1}}
{_id: "5c044d2bf4c6db53a047fe38", dateTime: "2018-12-02 22:22", userId: 5, event: "Failed to update Media Package sale", info: {userId: 5, AmountOfPrePaidItems: 0}}
{_id: "5c044d56f4c6db53a047fe39", dateTime: "2018-12-02 22:23", userId: 5, event: "Failed to update Media Package sale", info: {userId: 5, AmountOfPrePaidItems: 1}}
Using Atlas mongoDB 4.0.4
Edit 1:
.sort( _id: -1) also gives same output order