Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
How to replace string in mongodb collection with arrays

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 });
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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);
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!