Search code examples
ruby-on-railsmongodbmongoid

Increment in column for all records in one query with mongodb and rails


I have asked similar question here but same thing I need to do with mongodb how can I add increase every single record's created_at column. For example I have three records with timestamp 1,5 and 20 I want increment by 5 and these would become 6,10,25 respectively in one query. The purpose is to avoid loop for thousands of records.


Solution

  • You can use $inc operator from mongodb

    Rails usage

    CollectionName.collection.update_many({query}, {"$inc": {"field_name": value_to_inc_with}})