gpt4 book ai didi

node.js - 如何在 mongodb 中插入嵌套对象?

转载 作者:行者123 更新时间:2023-12-05 07:52:01 28 4
gpt4 key购买 nike

我有一个名为 bikes 的集合,如下所示:

{
"fname": "foo",
"indian":"hero-corps"
"brands": [
{
"region": "asia",
"type": "terrain"
}
]
}

然后像这样通过 post 获取一个 json(我们将其命名为 jsonBody):

{
"indian": "hero-corps",
"someKeyA": "someValueA",
}

我正在使用以下 mongo 更新查询:

 db.collection(bikes).update({"indian":"hero-corps"},{$set:jsonBody}, {upsert:true});

问题是它在主对象内部进行更新,我只想在嵌套对象 brands 中使用 jsonBody 进行更新。我该如何实现?

实际结果:

{
"fname": "foo",
"indian": "hero-corps",
"brands": [
{
"region": "asia",
"type": "terrain"
}
],
"indian": "hero-corps",
"someKeyA": "someValueA",
}

预期结果:

{
"fname": "foo",
"indian": "hero-corps",
"brands": [
{
"region": "asia",
"type": "terrain",
"someKeyA": "someValueA",
}
]
}

最佳答案

我不确定这是否是您想要做的,但我确定它会提供您想要的格式。

db.collection(bikes).update({"indian":"hero-corps"},{$push:{"brands":jsonBody}}, {upsert:true});

关于node.js - 如何在 mongodb 中插入嵌套对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34205553/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com