Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
Rails - mongoid - create and override using key?

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 !

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!