Finding most recent entry in a meteor collection

前端 未结 2 1421
情歌与酒
情歌与酒 2021-01-13 11:19

My task sounds simple, but I can\'t nail down how to write this... I simply want to find the most recent item inserted into a collection and display it on my meteor app.

相关标签:
2条回答
  • 2021-01-13 11:34

    I could only get the desired result by doing

    let commentsTmp: Comment = Comments.findOne({ parentId: this.currentPost._id }, { sort: { time: -1 } });
    

    with MongoObservable.Collection<T>

    Hope this will help someone.

    0 讨论(0)
  • 2021-01-13 11:57

    As per the docs you can also do:

    return Collection.findOne({}, {sort: {DateTime: -1, limit: 1}});
    

    Provided that DateTime is this field that you do Date.parse(Date()). Also you don't have to store dates as unix timestamps you can just do new Date() and it would be stored as the Date object type.

    0 讨论(0)
提交回复
热议问题