Home > Back-end >  iterate over large mongodb collection for purpose of updating schema
iterate over large mongodb collection for purpose of updating schema

Time:09-17

I have a 300k collection of test docs. I want to update all persons firstName and lastName to be lowercase.

  const person = new Schema({
  firstName: { type: String},
  lastName: { type: String }

})

I've added lowecase:true to the schema but how do I update the existing documents?

I tried:

  CaseFile
  .find({ })
  .cursor()
  .eachAsync(async function (doc) {
    await doc.save()
  })

but i get the error

Error: Collection method find is synchronous

I also tried :

   CaseFile
  .find({ })
  .then(docs => {
    docs.forEach(doc => {
      doc.save()
    })
  })

which gives the error:

JavaScript heap out of memory

db version v5.0.2

"mongoose": "^6.0.5",

thank you Wernfried Domscheit for the pipeline

  • Related