0

I have a collection of documents with a value property, I just want to multiply that by 0.75 and then .ceil() it.

I'm trying with

Model.update({}, { $mul: { value: 0.75 }})

but then I don't know how to round it.

I've seen aggregate allows to pipe operations but I don't understand if the output of aggregation is written in the DB or just returned, I need it to be updated in the DB.

Another solution would be to fetch all models, update them, and then save all of them but I don't know how to run a bulk save.

I have been pointed to this question but I don't see the relation since it's talking about referencing a property from inside the operation while I'm asking about how to round a property.

A. Matías Quezada
  • 1,886
  • 17
  • 34
  • I'm sorry @Neil Lunn but I don't see how is that a duplicate, I don't want to access the document, I just want to update it :\ – A. Matías Quezada Apr 24 '18 at 13:31
  • 1
    That's exactly what the answers describe. You cannot "Ceil" as an "update operator". The only way is to "read" the data an "write it back". Answers explain the "Bulk" update process for modern versions. – Neil Lunn Apr 24 '18 at 13:43
  • 1
    So the bulk is the only solution then, thanks for the explanation. – A. Matías Quezada Apr 24 '18 at 13:56
  • 1
    Short of writing a completely new collection that is. There are aggregation pipeline statements you can use, but you are still either processing a cursor and writing back results via separate statements, or you write to a "completely new" collection using `$out`. Just in case the answers are not clear on that. But they should be after I prodded some authors about it. – Neil Lunn Apr 24 '18 at 14:07

0 Answers0