【发布时间】:2021-07-01 11:59:53
【问题描述】:
假设我有一个看起来像这样的集合(本质上是一个包含对象的双嵌套数组):
{
'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)
关于如何解决这个问题的任何想法?
【问题讨论】:
-
这能回答你的问题吗? Inserting data to nested array in mongodb
标签: python mongodb pymongo pymongo-3.x pymongo-2.x