updateMany to assign default value in large collection

I am using updateMany to assign an empty array to null field in my document. I am planning to do this as a part of migration on server start.

db
.collection('largeCollectionWithMillionsOfDocuments')
.updateMany({ 'status.all': { $type: 10 } }, { $set: { 'status.all': [] } }, { upsert: false })

*Are there any limits to number of documents that can be updated by single updateMany?

Hi @Ajay_Mishra ,

There is no limitation per say, but I expect this approach to be not performant and may take a very long time.

What runs long can fail middle way and a restart of the process will be required.

Additionally, I don’t understand the upsert logic. If ita not type 10 you want to insert a document with only this value??

I found that doing this migration on application side as documents are read might be better. So every document that has a null value in this field will be updated on application side as empty arrays and gradually all documents will be in a new shape.

That is the power of the flexible model.

Thanks
Pavel

2 Likes

Thanks Pavel,

We are choosing the approach to update the model as its being read for performance reasons.
Since the updateMany itself could take a long time and also there is chance process could abort midway.

Regards
-Ajay