I am inserting data using this query:
database.insert({ postedAt: new Date() }, (error: any, doc: any) => {
if (error) {
console.log ('Error inserting record in the database: ', error);
} else {
console.log('Document: ', doc);
}
});
This is stored in the database:
{"postedAt":{"$$date":1557753437242},"_id":"PJL2N6hfkvKnTTRK"}
Then I want to find data sorted by latest input to show up first:
this.database.find({}).exec(function(err: any, docs: any) {
docs.forEach(function(d: any) {
console.log('Found user:', d);
});
});
Question 1: But how can I ensure I get only the latest record? Question 2: How can I get all records within 24 hours?
Thank you!
nedb supports sort by date out of the box, just sort it and limit 1
db.find({}).sort({postedAt: -1}).limit(1).exec((err, docs)=>{
console.log(docs[0]);
})