我有这个文档结构
{
"_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"
}
]
}
]
}
我想更新所有具有userId
of1
和handleName
of的对象John Doe
。请注意,对象可以具有相同的句柄名称但不同的用户 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"
}
]
}
]
}
这是如何实现的?
更新:我忘记了我还想更新另一个内部数组。