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

228
Visualizações
Sequence in mongo - can rely on findAndModify()?

I need some persistence integer Id for an operation called about 100 times a second. I have a single document in Settings collection where id is stored in lastId field.

As per mongodb doc I want to implement it using findAnyModify() operation with update: { $inc: { lastId: 1 } } parameter. Can I rely on that each next call returns a new (incremented) number?

Or is it better as a workaround to use java AtomicLong counter? It will read lastId from mongo on start, and on each increment I will write (but not read) from mongo?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you need a persisted sequence in mongodb, you indeed need to use the operator $inc in order to increment atomically on server side a given field that you will use as counter.

If you use an AtomicLong, you will hardly be able to guarantee the consistency of your sequence (especially in cluster environment) such that you could face situations where you end up with the same id for several documents.

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