Friday, 26 May 2017

Mongoose bulk update

How to update only one word in a field for all matched result?

const productSchema = new Schema({
  ...
  category: {type: String, required: true},
  tag : {type: String, required: true}
});

If user try to update the 'category' for products, the 'tag' will also be changed. However, 'tag' include many other words, it looks like ',category,title,price,storeId,...'. After, 'category' been changed, the corresponding word in 'tag' will also be changed.

I search the document in Mongoose, and find out:

Model.updateMany([criteria], [doc], [options], [callback])

This method can only replace the whole value of the field. Is there any other method can retrieve the value and replace parts of value for bulk updating?



via qwang

No comments:

Post a Comment