在不使用数字索引的情况下更新 mongodb 节点 js 中的双嵌套嵌入文档
Update double nested emedded documents in mongodb nodejs without using number index
我想更新 Itinirary,它是嵌入在
本身是嵌入式的包 document.I 尝试使用索引和
像这样成功了:
"$set":{'packages.$.itinerary.0.to': "kausaltar"}
但我不想在行程中使用像 0,1 这样的索引 update.Please 你能帮帮我吗?
我的 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" }}
我无法update.I不想使用像 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 }]
})
你可以用 $arrayfilter
db.getCollection("unit").update({_id: ObjectId("5e1ca76b9f96d17c449de177")}, { $set: { "packages.$[t].itinerary.$[d].from": "test" } },
{ arrayFilters: [ { "t.region": "ppopopop" }, {"d.id":15}]})
我想更新 Itinirary,它是嵌入在 本身是嵌入式的包 document.I 尝试使用索引和 像这样成功了:
"$set":{'packages.$.itinerary.0.to': "kausaltar"}
但我不想在行程中使用像 0,1 这样的索引 update.Please 你能帮帮我吗?
我的 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" }}
我无法update.I不想使用像 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 }]
})
你可以用 $arrayfilter
db.getCollection("unit").update({_id: ObjectId("5e1ca76b9f96d17c449de177")}, { $set: { "packages.$[t].itinerary.$[d].from": "test" } },
{ arrayFilters: [ { "t.region": "ppopopop" }, {"d.id":15}]})