Hi Team,
I have a collection with 5000k documents with multiple nested arrays.
I want to update the prodId key from old to new value.
Some of the documents have only 1 prodId value and other documents have one or more multiple prodId values and few of the documents have no prodId value.
- If I query like mentioned below–> I get all the prodId values available in that particular document and also the same value available in the other documents including other prodId.
db.test01.aggregate([{$unwind:"$ordDoc.custOrderItems"},
{$match:{"ordDoc.custOrderItems.custOrdSubItems.prodId":"VU0074"}},
{$project:{"ordDoc.custOrderItems.custOrdSubItems.prodId":1}}]).pretty()
output:
{
"_id" : "ORN-178450914676",
{
"ordDoc":{
"custOrdItems":{
"custOrdSubItems":[
{
"prodId":"VU0091"
},
{
"prodId":"VU0074"
},
{
"prodId":"VU0081"
},
{
"prodId":"VU0033"
},
{
"prodId": " "
},
{
"prodId":"VU0038"
}
]
}
}
}
- If i query like below i get the required prodId value for that value queried from all the documents. --> multiple $unwind command is used:
db.test01.aggregate([{$unwind:"$ordDoc.custOrderItems"},{$unwind:"$ordDoc.custOrderItems.custOrderSubItems"},{$match:{"ordDoc.custOrderItems.custOrderSubItems.prodId":"VU0074"}},{$project:{"ordDoc.custOrderItems.custOrderSubItems.prodtId":1}}]).pretty()
output:
{
"_id" : "ORN-12345678900096",
"ordDocument" : {
"custOrderItems" : {
"custOrderSubItems" : {
"prodId" : "VU0074""
}
}
}
},
I feel the 2nd method of querying is correct for this type of nested arrays. Correct me if I am wrong and based on this how to update one single prodId in all the documents where that prodId is available?
for example how to update prodId value from VU0074 to AC0067 available in all the 5000k documents?
Regards
Mam