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

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

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 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