Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda