假设我有一个看起来像这样的集合(本质上是一个包含对象的双嵌套数组):
{
'customer': 'bob',
'products': [
{
'id': 5,
'variants': [
{'name': 'blue',
'id': 23},
{'name': 'orange',
'id': 13},
{'name': 'green',
'id': 53},
]
}
]
},
{
'customer': 'dylan',
'products': [
{
'id': 5,
'variants': [
{'name': 'blue',
'id': 23},
{'name': 'green',
'id': 53},
]
}
]
}
我想删除以下所有variants
内容id
:[23, 53]
仅bob
{
'customer': 'bob',
'products': [
{
'id': 5,
'variants': [
{'name': 'orange',
'id': 13},
]
}
]
},
{
'customer': 'dylan',
'products': [
{
'id': 5,
'variants': [
{'name': 'blue',
'id': 23},
{'name': 'green',
'id': 53},
]
}
]
}
我有以下内容,但是它也删除了所有变体dylan
:
db.update({'$and': [{'user': 'bob'}, {'products': {'$elemMatch': {'id': 5}}}]}, {'$pull': {'products.$[].variants': {'id': {'$in': [23, 53]}}}}, False, True)
关于如何解决这个问题的任何想法?