Updating multiple objects based on a filter


#1

Is there an efficient way to update multiple objects of the same type given a filter?

Essentially something more efficient than:

var newObjs = [];

MyType.fetchObjsStream({filter: "property1 == 'value1'"}).each(function(obj) {
  newObjs.push(obj.putField('property2', 'value2'));
  if(newObjs.length >= 2000) {
    MyType.mergeBatch(newObjs);
    newObjs = [];
  }
});
if(newObjs.length > 0)
  MyType.mergeBatch(newObjs);

#2

See Persistable.mergeAll(obj, spec). The first argument is the obj with the fields that need to be merged. The second argument MergeAllSpec has a filter that you can provide to update the objects.


#3

Just to close this out, my code is now:

MyType.mergeAll({property2: ‘value2’}, {filter: Filter.eq(‘property1’, ‘value1’)})

Fantastic!!


#4

as an example, I just used this where the series header had a different unitConstraint to the unit.id on the actual measurement (update the header from some arbitrary value to “mm”):

WellStreamMeasurementSeries.mergeAll({unitConstraint:“mm”},{filter:“measurementType==‘Gas Flow Orifice Diameter’”})

works a charm.