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

204
Vistas
Updating multiple objects in an Array belonging to a collection

I'm using MERN stack for my program with mongoose for accessing the database. I have a collection called Movies and I wanted to edit multiple objects in an array within this collection. This is what the Movie Schema contains in my database:

enter image description here

I wanted to edit multiple objects in the 2D array within seats and to change isReserved to True.

I just used findOne in accessing the data since I still don't know how to update the objects that I want to access.

app.post('/confirm/:movieId/:timeId', (req, res) => { 
    const movieId = req.params.movieId;
    const timeId = req.params.timeId;
    const selectedSeats = req.body;
    // console.log("in confirm DB ");
    // console.log(selectedSeats);
    let getSeats;
    let getTimeSlots;
    const length_timeId = timeId.length;
    Movies.findOne({ movieId }, (err, movie) => {
        console.log("INSIDE");
        getTimeSlots = movie['timeslots'];
        let index = timeId.substring(1, length_timeId);

        //get the seats
        getSeats = getTimeSlots[parseInt(index)-1];
        //loop through seats 
        console.log("PRINTING GET SEATS");
        console.log(getSeats);
        for(var i=0; i<selectedSeats.length; i++) { 
            let row = parseInt(selectedSeats[i] / 5);
            let id = selectedSeats[i] % 5;
            console.log(getSeats["seats"][row][id]);
        }
    })
}) 

I already accessed the objects that I want to edit as that code displays this on my terminal: enter image description here

Would really appreciate some tips on how to update the isReserved status. Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Do you specify the timeslot and seat by an id or by the index within the array? If you use the index then solution is quite simple

const key = "timeslots." + req.params.timeId + ".seats." + req.body + ".isReserved";
var upd = {};
upd[key] = true;

db.Movies.updateOne({ movieId: req.params.movieId }, { $set: upd })

If your code uses the id then you have to work with array filters, see Update Nested Arrays in Conjunction with $[], so similar to this

db.Movies.updateOne(
   { movieId: req.params.movieId },
   { $set: { "timeslots.$[slot].seats.$[seat].isReserved": true } },
   { arrayFilters: [ { "slot.id": req.params.timeId } , { "seat.id": req.body } ] }
)
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