如果expirationDate
(博客文档中的一个字段)尚未超过当前日期,我正在寻找一种有效的方法来发布博客文章。
以下是一个简单的工作解决方案,但请阅读下面我的目标。
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}}});
}
我想知道是否有一种替代方案,我不需要“forEach”函数,但计算速度可能会更快。
例如,我可以实现npm node-cron-jobs
来检查expirationDate
是否没有超过服务器当前日期,如果是这样,只需将文档复制到“存档”集合并将其从博客集合中删除。
我可以使用 MongoDb 的 time to live但是,对于删除操作,我不知道是否或如何可以先将文档复制到另一个集合 - 这将是理想的解决方案。
最佳答案
只需创建使用$gt
的查询条件运算符来比较 expirationDate
字段大于当前日期的文档,即那些尚未过期的文档:
Meteor.publish('nonExpiredBlogs', function() {
var currentDate = new Date();
return Blog.find({"expirationDate": {"$gt": currentDate}});
}
关于node.js - Meteor:将文档复制到另一个集合并在 'expirationDate' 之后从原始集合中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30217432/