[英]mongodb: pull one item from nested array of references
我有一個嵌套的引用數組
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) } }
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.