我有一个嵌套的引用数组
const userSchema = new Schema(
{
_id: Schema.Types.ObjectId,
name: String,
posts: [{ type: Schema.Types.ObjectId, ref: "Post" }]
}
);
我想从该数组中删除一个引用,我认为使用这很容易
User.update({ name: currentName}, { $pull: { posts: postId }});
这个和类似的变体
User.update(
{ name: currentName},
{ $pull: { posts: mongoose.Types.ObjectId(postId) } }
);
或使用findOneAndUpdate
都对我不起作用。
postId
的格式例如 "5c150b855999681f7423aacb"
最佳答案
User.findOneAndUpdate({
_id: mongoose.Types.ObjectId('YOUR_DATA_ID')
},
{ name: currentName,
{ $pull: { posts: mongoose.Types.ObjectId(postId) } }
);
关于javascript - mongodb:从嵌套的引用数组中提取一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53794119/