gpt4 book ai didi

javascript - mongodb:从嵌套的引用数组中提取一项

转载 作者:行者123 更新时间:2023-12-03 00:32:33 24 4
gpt4 key购买 nike

我有一个嵌套的引用数组

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com