更新数组和内部数组中匹配的所有对象
Update all objects in array and inner array that match
我有这个文档结构
{
"_id": <OBJECT_ID>,
"orders": [
{
"userId": 1,
"handleName": "John Doe",
"orderAmount": 3,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 2,
"handleName": "Maria Gigante",
"orderAmount": 2,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 1,
"handleName": "John Doe",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 3,
"handleName": "John Doe",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
}
]
}
我想更新 1
的 userId
和 John Doe
的 handleName
的所有对象。
请注意,对象可以具有相同的句柄名称但不同的用户 ID。这就是为什么我需要先将它与 userId
匹配。
例如我想将句柄名称更改为Donald Stark
我想要这样的结果:
{
"_id": <OBJECT_ID>,
"orders": [
{
"userId": 1,
"handleName": "Donald Stark",
"orderAmount": 3,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 2,
"handleName": "Maria Gigante",
"orderAmount": 2,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 1,
"handleName": "Donald Stark",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 3,
"handleName": "John Doe",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
}
]
}
这是如何实现的?
更新:
我忘了我还有另一个我也想更新的内部数组。
这样的事情能解决您的问题吗?
d = {
"_id": <OBJECT_ID>,
"orders": [
{
"userId": 1,
"handleName": "John Doe",
"orderAmount": 3
},
{
"userId": 2,
"handleName": "Maria Gigante",
"orderAmount": 2
},
{
"userId": 1,
"handleName": "John Doe",
"orderAmount": 4
},
{
"userId": 3,
"handleName": "John Doe",
"orderAmount": 4
}
]
}
def change_name(usr_id, old_name, new_name):
for usr in d['orders']:
if usr['userId'] == usr_id and usr['handleName'] == old_name:
usr['handleName'] = new_name
change_name(1, 'John Doe', 'Donald Stark')
我们可以使用$[identifier]运算符来更新数组中的特定对象
所以您的更新查询应该类似于
db.collection.updateOne(
{ _id: <OBJECT_ID> }, // filter part, add the real objectId here
{ $set: { 'orders.$[order].handleName': 'Donald Stark' } }, // update part
{ arrayFilters: [{ 'order.userId': 1, 'order.handleName': 'John Doe' }]
})
$[order] 仅更新与数组 filters
中的条件匹配的对象
更新
如果你有一个内部数组,并且你需要对内部数组中的元素做同样的事情,我们可以使用类似的东西
db.collection.updateOne(
{ _id: <OBJECT_ID> }, // filter part, add the real objectId here
{ $set: {
'orders.$[order].handleName': 'Donald Stark', // to set the handleName in the elements of the outer array
'orders.$[].others.$[other].handleName': 'Donald Stark' // to set the handleName in the elements of the inner array
} },
{ arrayFilters: [{ 'order.userId': 1, 'order.handleName': 'John Doe' }, { 'other.userId': 1, 'other.handleName': 'John Doe' }] }
)
我们使用$[]
遍历订单数组中的所有元素,然后使用$[other]
根据数组过滤器[=15]中的新条件更新内部数组中的特定元素=]
希望对您有所帮助
我有这个文档结构
{
"_id": <OBJECT_ID>,
"orders": [
{
"userId": 1,
"handleName": "John Doe",
"orderAmount": 3,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 2,
"handleName": "Maria Gigante",
"orderAmount": 2,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 1,
"handleName": "John Doe",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 3,
"handleName": "John Doe",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "John Doe"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
}
]
}
我想更新 1
的 userId
和 John Doe
的 handleName
的所有对象。
请注意,对象可以具有相同的句柄名称但不同的用户 ID。这就是为什么我需要先将它与 userId
匹配。
例如我想将句柄名称更改为Donald Stark
我想要这样的结果:
{
"_id": <OBJECT_ID>,
"orders": [
{
"userId": 1,
"handleName": "Donald Stark",
"orderAmount": 3,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 2,
"handleName": "Maria Gigante",
"orderAmount": 2,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 1,
"handleName": "Donald Stark",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
},
{
"userId": 3,
"handleName": "John Doe",
"orderAmount": 4,
"others": [
{
"userId": 1,
"handleName": "Donald Stark"
},
{
"userId": 2,
"handleName": "Maria Gigante"
}
]
}
]
}
这是如何实现的?
更新: 我忘了我还有另一个我也想更新的内部数组。
这样的事情能解决您的问题吗?
d = {
"_id": <OBJECT_ID>,
"orders": [
{
"userId": 1,
"handleName": "John Doe",
"orderAmount": 3
},
{
"userId": 2,
"handleName": "Maria Gigante",
"orderAmount": 2
},
{
"userId": 1,
"handleName": "John Doe",
"orderAmount": 4
},
{
"userId": 3,
"handleName": "John Doe",
"orderAmount": 4
}
]
}
def change_name(usr_id, old_name, new_name):
for usr in d['orders']:
if usr['userId'] == usr_id and usr['handleName'] == old_name:
usr['handleName'] = new_name
change_name(1, 'John Doe', 'Donald Stark')
我们可以使用$[identifier]运算符来更新数组中的特定对象
所以您的更新查询应该类似于
db.collection.updateOne(
{ _id: <OBJECT_ID> }, // filter part, add the real objectId here
{ $set: { 'orders.$[order].handleName': 'Donald Stark' } }, // update part
{ arrayFilters: [{ 'order.userId': 1, 'order.handleName': 'John Doe' }]
})
$[order] 仅更新与数组 filters
中的条件匹配的对象更新
如果你有一个内部数组,并且你需要对内部数组中的元素做同样的事情,我们可以使用类似的东西
db.collection.updateOne(
{ _id: <OBJECT_ID> }, // filter part, add the real objectId here
{ $set: {
'orders.$[order].handleName': 'Donald Stark', // to set the handleName in the elements of the outer array
'orders.$[].others.$[other].handleName': 'Donald Stark' // to set the handleName in the elements of the inner array
} },
{ arrayFilters: [{ 'order.userId': 1, 'order.handleName': 'John Doe' }, { 'other.userId': 1, 'other.handleName': 'John Doe' }] }
)
我们使用$[]
遍历订单数组中的所有元素,然后使用$[other]
根据数组过滤器[=15]中的新条件更新内部数组中的特定元素=]
希望对您有所帮助