I would like to know if there is any simpler (and faster) possibilities to create/override an item in my mongo databse by looking at a Key.
Let me explain :
MyObject:
#<MyObject _id: 5881e885049490276d1d85b9,
data_table: [...],
my_key: "test">
so this is my object
Today, I need to modify a lot of theses object in a very fast way (very often) updating the data_table
Today I do this :
ct = MyObject.where(my_key: "test").first
ct.update_attributes(data_table: data)
But I think I lose a lot of performances, right ?
There is a way I can do like a create or update using the key my_key to create or override the item ?
Or any other ideas to improves performances ?
thanks !
Try this:
MyObject.find_or_create_by(my_key: "test").update_attributes(data_table: data)
In this case there is no any performance optimizations from mongo side, it will again query db, if not found will create a new record and will update it - just in one line.
Another optimization would be to replace update_attributes with set if you don't need to run validations on your model.