如何删除mongoose的子文件?

我有一个拥有许多文档的集合,每个文档都有一个子文档数组

comment : { _id : ObjectId name : String, comments : [commentSchema] } commentSchema : { _id : ObjectId, commentType : String // can be either 'string' or 'image' commentData : String } 

现在,我想从整个评论集合中删除“评论”的评论types=“图像”的path中的所有子文档。

我做了以下

 export.removeComments = function(next) { mongoose.model('comment').find({}, function(err,docs){ if(err) return next(err); docs.forEach(function(doc, index){ doc.comments.pull({ commentType : 'image'}); //Following also not works /* var comments = doc.comments; for(var i =0; i < comments.length; i++ ) { if(comments[i].commentType == 'image') doc.comments.remove(comments[i]._id); }*/ if(index < docs.length - 1) doc.save(); else doc.save(next); }); }); }; 

但以上没有影响。

任何帮助?

以下为我工作:

只是添加toString()与相应的subdocument id,因为它是一个对象不是一个string。 也许删除子文档需要id的string。 纠正我,如果我错了。

 mongoose.model('comment').find({}, function(err,docs){ if(err) return next(err); docs.forEach(function(doc, index){ for(var i =0; i < comments.length; i++ ) { if(comments[i].commentType == 'image') doc.comments.remove(comments[i]._id.toString()); } if(index < docs.length - 1) doc.save(); else doc.save(next); }); });