我的数据库 struct 如下,它只有一个document
和products
中的nested array
,它由ref
到product document's id
组成.
{
_id:"6371e078393a5194cc674369"
data: Array
[0]:{
image:
title:
products: Array
[0]:product1Id
[1]:product2Id
[2]:product3Id
},
[1]:{
image:
title:
products: Array
[0]:product2Id
[1]:product3Id
[2]:product4Id
},
}
我的要求是,当我从product document
中删除product3
时,我也想删除它的引用.所以在这里,我想从嵌套的array
中删除product3Id
.
更新后的文档将如下所示:
{
_id:"6371e078393a5194cc674369"
data: Array
[0]:{
image:
title:
products: Array
[0]:product1Id
[1]:product2Id
},
[1]:{
image:
title:
products: Array
[0]:product2Id
[1]:product4Id
},
}
我的try :
result = await homeSpecialModel.updateMany(
{
_id: "6371e078393a5194cc674369",
},
// { $pull: { data: { products: { $eleMatch: new ObjectId(id) } } } } -- 1st try
// { $pull: { "data.products": new ObjectId(id) } } -- 2nd try
);
两个看起来都不管用!