Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda