meteor:将文档复制到另一个集合,并在'expirationDate'之后从原始集合中删除

如果expirationDate (这是博客文档中的一个字段)没有通过当前date,我正在寻找一种有效的方式来发布博客文章。

以下是一个简单的工作解决scheme,但请阅读下面我想要做的。

 Meteor.publish('nonExpiredBlogs', function() { var blogIds = [] var currentDate = new Date() Blogs.find().forEach(function(doc) { var expirationDate = doc.expirationDate var hasExpDatePassed = (expirationDate - currenDate) < 0 if (hasExpDatePassed === false) { // expiration date is Not passed, get the doc _id blogIds.push(doc._id) } }); return Blog.find({_id: {$in: {_id: blogIds}}}); } 

我想知道是否有一个替代scheme,我不需要一个“forEach”函数,可能会更快计算。

例如,我可以实现npm node-cron-jobs来检查expirationDate是否没有通过服务器的当前date,如果是,只需将文档复制到“存档”集合并从博客集合中删除它。

我可以使用MongoDb的时间来进行删除操作,但是,我不知道是否或如何将文档复制到另一个集合 – 这将是理想的解决scheme。

只需创build使用$gt操作符来比较expirationDate字段大于当前date的文档(即那些尚未过期的文档)的查询条件:

 Meteor.publish('nonExpiredBlogs', function() { var currentDate = new Date(); return Blog.find({"expirationDate": {"$gt": currentDate}}); }