Mongoose:覆盖更新的数组属性

如何覆盖基于给定数组作为源文档的数组属性?

架构:

var postSchema = new mongoose.Schema({ title: { type: String, required: true, index: { unique: true } }, content: { type: String }, tags: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Tag' }] }); 

我现在有一个标签对象的ID数组,我想覆盖标签属性。 我现在遇到的问题是,它添加了新的标签,但它不会删除不在源数组中的标签。

我正在使用findOneAndUpdate执行更新,如下所示:

 // Pseudo code example Post.findOneAndUpdate({ _id: id }, { tags: ["id1...", "id2..."], {}, cb); 

你试过$ set操作符吗?

Post.findOneAndUpdate({ _id: id }, { $set: {tags: ["id1...", "id2..."]}, {}, cb);