Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!