This is what we are looking to update
db.getCollection('location').find({"correlationId.correlation":"MN09"})
Result:
{
"_id" : ObjectId("4523452345235234"),
"correlationId" : {
"correlation" : "MN09"
},
"sortSequence" : 120,
"code" : "MN09",
"description" : "Manhattan 9",
"legacyZone" : "West",
"locationTypeId" : {
"correlation" : "DISTRICT"
},
"garageCode" : "MW09G",
"servicesSelf" : true,
"servicedBy" : [
{
"correlation" : "MN12"
}
],
"workUnit" : {
"correlation" : "MNBO"
}
}
Result:
{
"_id" : ObjectId("4523452345235234"),
"correlationId" : {
"correlation" : "MN09"
},
"sortSequence" : 120,
"code" : "MN09",
"description" : "Manhattan 9",
"legacyZone" : "West",
"locationTypeId" : {
"correlation" : "DISTRICT"
},
"garageCode" : "MW09G",
"servicesSelf" : true,
"servicedBy" : [
{
"correlation" : "MN12"
}
],
"workUnit" : {
"correlation" : "MNBO"
}
}
we would like to do a find and update on
"servicedBy" : [
{
"correlation" : "MN12"
}
]
{
"correlation" : "MN12"
}
]
using
db.getCollection('location').update(
{"correlationId.correlation":"MN09"},
{ $set : { "servicedBy.$[elem].correlation" : "MN04"}},
{ arrayFilters: [{ "elem.correlation" : {$eq: "MN12"} }], multi:true}
)
db.getCollection('location').update(
{"correlationId.correlation":"MN09"},
{ $set : { "servicedBy.$[elem].correlation" : "MN04"}},
{ arrayFilters: [{ "elem.correlation" : {$eq: "MN12"} }], multi:true}
)
now we query again
db.getCollection('location').find({"correlationId.correlation":"MN09"})
Result:
{
"_id" : ObjectId("4523452345235234"),
"correlationId" : {
"correlation" : "MN09"
},
"sortSequence" : 120,
"code" : "MN09",
"description" : "Manhattan 9",
"legacyZone" : "West",
"locationTypeId" : {
"correlation" : "DISTRICT"
},
"garageCode" : "MW09G",
"servicesSelf" : true,
"servicedBy" : [
{
"correlation" : "MN04"
}
],
"workUnit" : {
"correlation" : "MNBO"
}
}
Result:
{
"_id" : ObjectId("4523452345235234"),
"correlationId" : {
"correlation" : "MN09"
},
"sortSequence" : 120,
"code" : "MN09",
"description" : "Manhattan 9",
"legacyZone" : "West",
"locationTypeId" : {
"correlation" : "DISTRICT"
},
"garageCode" : "MW09G",
"servicesSelf" : true,
"servicedBy" : [
{
"correlation" : "MN04"
}
],
"workUnit" : {
"correlation" : "MNBO"
}
}
ref:
https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#up._S_[%3Cidentifier%3E]
Comments
Post a Comment