gpt4 book ai didi

node.js - 在mongodb Node js中更新双重嵌套嵌入文档而不使用数字索引

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:49 24 4
gpt4 key购买 nike

我想更新嵌入子文档中的行程 包本身就是一个嵌入式文档。我尝试使用索引和 像这样成功了:

 "$set":{'packages.$.itinerary.0.to': "kausaltar"} 

但我不想在行程更新中使用像 0,1 这样的索引。请您帮助我。

我的 JSON 架构是这样的:

  {
"_id" : ObjectId("5e1ca76b9f96d17c449de177"),
"org_id" : "22222222222",
"packages" : [
{
"_id" : ObjectId("5e1ca76b9f96d17c449de17a"),
"region" : "ppopopop",


"itinerary" : [
{
"_id" : ObjectId("5e1ca76b9f96d17c449de17d"),
"id" : 1,
"from" : "ppopopop",
"to" : "ashinnkn",
"mode" : "4444444444",
"day" : 2,
"duration_hrs" : 3
},
{
"_id" : ObjectId("5e1ca76b9f96d17c449de17c"),
"id" : 2,
"from" : "44444444444",
"to" : "Faketon2",
"mode" : "4444444444",
"day" : 2,
"duration_hrs" : 3
},
{
"_id" : ObjectId("5e1ca76b9f96d17c449de17b"),
"id" : 3,
"from" : "55555555555",
"to" : "ashin",
"mode" : "55555555",
"day" : 2,
"duration_hrs" : 3
}
],
"image_url" : "sadfasfsa",
},
{
"_id" : ObjectId("5e1ca76b9f96d17c449de178"),
"region" : "bktll",
"itinerary" : [
{
"_id" : ObjectId("5e1ca76b9f96d17c449de179"),
"id" : 1,
"from" : "mmkkkkm",
"to" : "ashin",
"mode" : "SkkkkA",
"day" : 2,
"duration_hrs" : 3
}
],
"image_url" : "sadfasfsa",
}
]
}
}

我想更新包内的行程字段,它本身就是一个嵌入文档。 我的代码是这样的:

 AgentPackage.update({
"_id" : mongoose.Types.ObjectId("5e1ca76b9f96d17c449de177"),
"packages.region" : "ppopopop",
'packages.itinerary.id': 2,
}, {$set: {'packages.itinerary.$$.from': "test" }}

我无法更新。我不想使用像 0,1 INSIDE 查询这样的索引。

最佳答案

试试这个:

如果您的包内只有一个包含 region : "ppopopop" 的对象,请尝试此操作(在大多数情况下这应该足够了,因为您可能没有同名的重复区域):

AgentPackage.update({
_id: ObjectId("5e1ca76b9f96d17c449de177"), "packages.region": "ppopopop"
}, { $set: { "packages.$.itinerary.$[item].to": 'kausaltar' } }, {
arrayFilters: [{ 'item.id': 2 }]
})

如果您的包内有多个带有 region : "ppopopop" 的对象,请尝试以下操作:

AgentPackage.update({
_id: ObjectId("5e1ca76b9f96d17c449de177"), "packages.region": "ppopopop" // Here this "packages.region": "ppopopop" is optional as _id will only bring one document else might be helpful to bring only docs with region: ppopopop right after filter.
}, { $set: { "packages.$[eachPackage].itinerary.$[eachItinerary].to": 'kausaltar' } }, {
arrayFilters: [{ 'eachPackage.region': "ppopopop" }, { 'eachItinerary.id': 2 }]
})

引用: update-positional-filtered

关于node.js - 在mongodb Node js中更新双重嵌套嵌入文档而不使用数字索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59722477/

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