Nedb 如何按日期排序查找查询?

Nedb 如何按日期排序查找查询?,nedb,Nedb,我正在使用此查询插入数据: 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); } });

我正在使用此查询插入数据:

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);
      }
    });
这将存储在数据库中:

{"postedAt":{"$$date":1557753437242},"_id":"PJL2N6hfkvKnTTRK"}
然后我想查找按最新输入排序的数据,以便首先显示:

    this.database.find({}).exec(function(err: any, docs: any) {  
      docs.forEach(function(d: any) {
          console.log('Found user:', d);
      });
    });
问题1:但我如何确保我只获得最新记录? 问题2:如何在24小时内获取所有记录


谢谢大家!

nedb支持开箱即用的按日期排序,只需将其排序并限制为1即可

db.find({}).sort({postedAt: -1}).limit(1).exec((err, docs)=>{
    console.log(docs[0]);
})

试着解决第一个问题@JuliaShestakova谢谢。