提问人:Vaodi 提问时间:1/23/2023 更新时间:1/23/2023 访问量:37
Mongo db 更新嵌套数组中包含许多元素的嵌套数组
Mongo db update a nested array with many elements in a nested array
问:
我有一个看起来像这样的数据库:
{
"_id": "637c648fb8fcfb2bc3071bb9",
"consultant_name": "Sam smith",
"consultantUsername": "sam",
"consultant_Password": "123",
"type": "consultant",
"clients": [
{
"client_name": "john",
"client_Username": "mouh",
"client_Password": "123",
"type": "client",
"documents": [
{
"name": "Passeport",
"description": "copie conforme certifié de tout le passeport",
"doc_upload": "Capture dâeÌcran, le 2022-11-28 aÌ 23.01.12.png",
"_id": "637c648fb8fcfb2bc3071bbb"
},
{
"name": "Acte de naissance",
"description": "Pour prouver la filiation",
"doc_upload": "_Abstract Aesthetic CD Album Cover Art.png 637c648fb8fcfb2bc3071bbc",
"_id": "637c648fb8fcfb2bc3071bbc"
}
],
"_id": "637c648fb8fcfb2bc3071bba"
},
如您所见,Sam smith 有不同的客户,每个客户都有不同的文档。我的目标是能够允许 Sam 再添加一个客户端到他的投资组合中,并指定该新用户的文档(他想要的文档数量)一次创建(当 Sam 在数据库中创建新用户时)。我使用的过程是通过创建一个新客户端来更新他的客户列表。
这是添加客户端的代码(有效),请注意文档部分不会更新:
router.put("/:id", async(req,res)=>{
try {
const updateUser = await User.findByIdAndUpdate(req.params.id, {
$push: {
clients:{
client_name: req.body.client_name,
client_Username: req.body.client_Username,
client_Password: req.body.client_Password,
//documents not updating even with one single entry
documents : [
{
name : req.body.docName,
description : req.body.docDescription,
doc_upload : req.body.doc_upload,
}
]
}
}
},{new:true});
res.status(200).json(updateUser);
}
catch(err) {
res.status(500).json(err);
}
});
所以我的直觉是使用 $each 运算符推送文档,但由于文档更新不起作用,我有点卡住了。在理想情况下,如果您有答案/反思能够用多个值更新文档部分,我们将不胜感激。关于该做什么或我应该先看哪里的任何想法?
答: 暂无答案
下一个:更新嵌套数组中的嵌套数组
评论