My FoodRecord records contains something like this:
"_modifierRecords" : [
"com.domain.data.REM.FoodModifierRecord:57919e6930049c15986e7c05"
"com.domain.data.REM.FoodModifierRecord:57919e6930049c15986e7c06"
],
"_discountModifierRecords" : [
],
"_salesRecord" : "com.domain.data.REM.SalesRecord:57919e6930049c15986e7c01"
I need to replace com.domain.data.REM. to just com.domain.data. if they exists in the 3 fields above.
This is what I have so far but I'm having problems:
var operations = [];
db.FoodRecord.find().forEach(function(doc) {
var operation = {
updateOne: {
filter: { '_id': doc._id },
update: {
'$set': { '_salesRecord': doc._salesRecord.replace('com.domain.data.REM.SalesRecord:', 'com.domain.data.SalesRecord:'),
'_modifierRecords.$' : doc._modifierRecords.$.replace('com.domain.data.REM.FoodModifierRecord:', 'com.domain.data.FoodModifierRecord:') }
}
}
};
operations.push(operation);
})
operations.push({
ordered: true,
writeConcern: { w: "majority", wtimeout: 5000 }
})
db.FoodRecord.bulkWrite(operations, { ordered : false });
That solution seems to work, but that's probably not the optimal one.
db.FoodRecord.find(/* your query */).forEach(function(e,i){
var newArr = [];
e._salesRecord.forEach(function(e,i) {
newArr.push(e.replace('REM.SalesRecord','SalesRecord'));
});
e._salesRecord = newArr;
newArr = [];
e._modifierRecords.forEach(function(e,i) {
newArr.push(e.replace('REM.FoodModifierRecord','FoodModifierRecord'));
});
e._modifierRecords = newArr;
db.FoodRecord.save(e);
});