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

106
Vistas
Update hotel room reservation array - mongoose

I'm working on a hotel room reservation system. Following are attributes in Room model.

rId: String,
        allocation: [{
            date: Number, // 210403 == 2021-04-03
            slots: [{
                type: mongoose.Schema.Types.Mixed,
                ref: 'reservation',
                maxItems: 48
            }]
        }],
        _active: Boolean

This "slots" is an array with 48 items(30mins time slots).Here I want to update this array with user inputs.As an example if a user wants to reserve a time between 2.30 - 3.30 i want to update that two slots in array to "reserved". Following code is the controller for reservation

reservation
        .save(reservation) //Save reservation data
        .then((data) => { // Update room slots
            var dt = req.body.checkInDate.substr(0, 10); // extract date from user input
            var tm = req.body.checkInDate.substr(11, 5); // extract time
            dt = dt.replace(/-/g, '');  // 210403 == 2021-04-03
            tm = tm.replace(/:/g, '.');
            console.log(tm)
            console.log(dt)
            Room.findOneAndUpdate({ rId: rId }, { //Update time slot
                $push: {
                    allocation: [{
                        date: dt,
                        slots: [{ tm }]
                    }]
                }
            })

Above code is not working. Can someone help me with this?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Demo - https://mongoplayground.net/p/bn1lJpZzhUT

https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/

db.collection.update(
  { rId: 1, "allocation.date": 20190701 },
  { $push: { "allocation.$[a].slots": { a: 1, b: 2 } } },
  { arrayFilters: [ { "a.date": 20190701 } ]}
)
over 4 years ago · Santiago Trujillo 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