Meteor:将文档复制到另一个集合并在 'expirationDate' 之后从原始集合中删除
Meteor: copy document to another collection and delete from original collection after 'expirationDate'
如果 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
是否没有超过服务器当前日期,如果是,只需将文档复制到 'Archive' 集合并将其删除来自博客集合。
我可以使用 MongoDb 的 time to live 进行删除操作,但是,我不知道是否可以或如何先将文档复制到另一个集合 - 这将是理想的解决方案。
只需创建使用 $gt
运算符的查询条件来比较 expirationDate
字段大于当前日期的文档,即尚未过期的文档:
Meteor.publish('nonExpiredBlogs', function() {
var currentDate = new Date();
return Blog.find({"expirationDate": {"$gt": currentDate}});
}
如果 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
是否没有超过服务器当前日期,如果是,只需将文档复制到 'Archive' 集合并将其删除来自博客集合。
我可以使用 MongoDb 的 time to live 进行删除操作,但是,我不知道是否可以或如何先将文档复制到另一个集合 - 这将是理想的解决方案。
只需创建使用 $gt
运算符的查询条件来比较 expirationDate
字段大于当前日期的文档,即尚未过期的文档:
Meteor.publish('nonExpiredBlogs', function() {
var currentDate = new Date();
return Blog.find({"expirationDate": {"$gt": currentDate}});
}