查找流星集合中的最新条目 [英] Finding most recent entry in a meteor collection

查看:47
本文介绍了查找流星集合中的最新条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务听起来很简单,但我无法确定如何编写它...我只想找到插入到集合中的最新项目并将其显示在我的流星应用程序上.

我一直在玩 Collection.find() 没有真正的结果.这是我试过的最后一行:(请记住这些名称是占位符.我的集合在我的代码中不称为集合)

<块引用>

Template.tempname.tempitem = function () {返回 Collection.find({}, {sort: {DateTime: -1, limit: 1,}});};

我在集合中有一个字段是

的结果<块引用>

Date.parse(Date())

这应该允许我按最近排序.可能还有其他方法可以做到这一点,但这是我想到的第一个解决方案.

解决方案

根据 docs 您还可以执行以下操作:

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

前提是DateTime 是您执行Date.parse(Date()) 的这个字段.此外,您不必将日期存储为 unix 时间戳,您只需执行 new Date() 并将其存储为 Date 对象类型.

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.

I've been playing with Collection.find() with no real results. This is the last line I've tried: (keep in mind these names are placeholder. My collection is not called Collection in my code)

Template.tempname.tempitem = function () {
    return Collection.find({}, {sort: {DateTime: -1, limit: 1,}});
};

I have a feild in the collection that is the result of

Date.parse(Date())

which should allow me to sort by most recent. There is likely other ways to do this, but this was the first solution I came up with.

解决方案

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.

这篇关于查找流星集合中的最新条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆