【发布时间】:2012-08-20 23:44:04
【问题描述】:
我有一个看起来有点像的架构:
var postSchema = new Schema({
created: { type: Date, default: Date.now },
updated: { type: Date, default: Date.now },
comments: { type: [Schema.ObjectId], ref: 'Comment' }
});
所以我的 cmets 集合是引用我的评论模式/集合的对象 id 的集合。
我需要在查询时删除其中一些,所以我正在尝试这个:
var comments = [1, 2, 4];
Post.update({ _id: post_id}, {'$pullAll': {comments: comments }})
.exec(function(err) {
// How to remove documents with 1, 2, 4 ids from a Comment collection properly
});
执行上面的代码后,我从 Post.cmets 中删除了评论的 id,但我还需要从“评论”集合中删除这些 cmets。我该怎么做?
编辑:我怎样才能得到实际上没有被删除的文档的 ID。简单的例子:
Post.comments = [1, 2, 3];
Post.update({ _id: post_id}, {'$pullAll': {comments: [1,2]}});
在上面的代码中 Post.cmets 只有 1,2,3,但我们试图拉 [1,2],所以我需要知道 Post.cmets 中不存在 id=3 而我不知道'不需要从'评论'集合中删除它。
【问题讨论】: