{
"_id": ObjectId("60743bd0ffb98b1e7c215c1b"),
"orderId": "787968",
"notes":"Leave in porch"
"lineItems": [
{
"lineItemId": "1741547",
"channelLineItemId": "9741370294381",
"skuId": "XXX-YYY-x-PR-YYY"
},
{
"lineItemId": "1741549",
"channelLineItemId": "9741370359917",
"skuId": "XXX-YYY-x-PR-YYY"
},
{
"lineItemId": "1741551",
"channelLineItemId": "9741370425453",
"skuId": "XXX-YYY-x-P-YYY"
}
],
"Data": {
"email_status": "SENT",
"sent_date": ISODate("2021-04-12T12:23:48.623Z")
}
}
I had a Collection orderData in my mongo with the above structure.
I need to replace all -PR- and -PG- with a simple hyphen - in all records and in all lineitems. ie, it needs to loop through all the records and all the array elements.
if I am using a SQL command I will be using
update line_item set sku_id = replace(sku_id , '-PR-','-');
after executing the command the XYZ-BCD-X-PR-ZAB will be XYZ-BCD-X-AB
I know how to update the elements outside to the array, if I am trying to replace command in notes I can use
db.orderData.find().forEach(function(doc) {
doc.note : doc.note.replace('-PG-', '-');
db.orderData.save(doc);
});
I don't know how to use the same in updating in an array
I had tried
db.orderData.find({orderId : "787968"}).forEach(function(doc) {
doc.lineItems.$.skuId : doc.lineItems.$.skuId.replace('-PG-', '-');
db.orderData.save(doc);
});
but it is not working.
Someone, please help me and thanks in advance.
skuId?