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

157
Visualizações
Insert document only if valid with the rest of collection

I am looking to implement a "sauna reservation" with mongodb. For this I want to have a reservation collection with each reservation containing a start-time and duration in minutes.

BEFORE inserting a reservation upon request, I'd like to make sure that its start + duration does not conflict any other reservation thats already in the collection.

I can not think of a way to implement this in a query (if thats even possible), so I guess I have to:

  1. retrieve collection (only the reservations for today)

  2. iterate over the response from the first query and make sure the reservation I want to insert is valid, nonconflicting. Then I need to

  3. save the reservation.

HOWEVER how do I make sure that between step 1 and 3 there is not another session that inserted a reservation that conflicts with the current one?

Doing step 1 to 3 blindly in code would surely result in broken data/conflicting reservations.

Does anyone have an idea how to solve this with mongodb?

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

0

One solution is to use a FIFO (First In First Out) queue system. Every time you have a new reservation coming in it will come in to a queue. The system will then pull items from the front of the queue one by one and only process one at a time. in your case the process will be the steps 1 - 3. You can also improve step 1 & 2 by only querying mongoDB by using a find where the start time of the appointment and end time of the appointment don't clash with another appointment. This way you don't need to do an expensive operation like retrieving ALL the reservations for today and iterating through them.

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