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

88
Views
Mongoose / MongoDB query

help me, I am stuck at working on a functionality which helps a user to book a limited seats in any hall. So my logic is that, I generate a document every time i got input{no of seats to be booked}. So Initially if no of documents is zero than all the seats are available and seat counter starts with s1 which gives each seat unique identification. All this going right but when I give Input for the very first time it says no seats available and in the second input it makes document with number of seats booked{input number} and array containing seat numbers generated from "S1" and goes up-to limited number given. After this the next problem came is after giving input third time it again generate document with S1 and which is already generated in document before. After this i gave any no of inputs it goes fine until limit exceed. Here is my code.

 const { name, seats, message } = req.body;
  var query = reserve.find({});
  query.count((err, count) => {
    if (err) throw err;
    if (count === 0) {
      seatStart = 1;
      availableSeats = 80;
      count++;
    }
  });

  if (availableSeats >= seats && seats > 0) {
    var seatNo = [];
    for (var i = 0; i < seats; i++) {
      seatNo.push("S" + seatStart);
      seatStart++;
    }
    availableSeats -= seats;
    mong.Reserve.create(
      {
        name: name,
        seats: seats,
        seatNo: seatNo,
        message: message,
      },
      (err, data) => {
        if (err) throw err;
        console.log(data);
      }
    );
    res.send("success");
  } else {
    if (availableSeats == 0) {
      res.send("not available !! all booked");
    } else {
      res.send("not available !! only " + availableSeats + " seats available");
    }
  }

Output Output

about 4 years ago · Juan Pablo Isaza
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!