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

247
Visualizações
JS: ordinate access to mongoDB

i have a little problem, i thought how to resolve it but i don't know if it's the right way.

in my project I have several functions, all of which must use a sequential unique id every time a function is executed

for example:

person 1 runs function X using id 1
person 2 runs function Y using id 2
person 3 runs function Z using id 3
person 4 runs function Y using id 4
person 1 runs function Z using id 5
....

how to do that?

I thought to save that id in a table inside mongoDB and every time it will used inside a function, the function will increment it, but i don't think it is the right way, it is?

then, if it is the right way, how to co-ordinate the access to the DB? like if 2 people runs 2 functions in the same time, the will take the same ID from DB and this absolutely must not happen, every time a function is called a different id must be used, how can I do?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use findOneAndUpdate

Example:

db.collection.findOneAndUpdate(
   { "name" : "function_trigger_count" },
   { $inc: { "count: 1 } }
)

FindOneAndUpdate ensure the atomicity of the query

OR

If you are using MongoDB version 5.x you can use Transaction feature to ensure the atomicity

about 4 years ago · Juan Pablo Isaza 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