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

98
Views
Cómo reemplazar una cadena en la colección mongodb con matrices

Mis registros de FoodRecord contienen algo como esto:

 "_modifierRecords" : [ "com.domain.data.REM.FoodModifierRecord:57919e6930049c15986e7c05" "com.domain.data.REM.FoodModifierRecord:57919e6930049c15986e7c06" ], "_discountModifierRecords" : [ ], "_salesRecord" : "com.domain.data.REM.SalesRecord:57919e6930049c15986e7c01"

Necesito reemplazar com.domain.data.REM. a solo com.domain.data. si existen en los 3 campos anteriores.

Esto es lo que tengo hasta ahora, pero estoy teniendo problemas:

 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

Esa solución parece funcionar, pero probablemente no sea la óptima.

 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!