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

131
Views
mongoose add array to subdocuments

I have a signup shema:

const mongoose = require("mongoose");

const signupSchema = mongoose.Schema({
  userId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
  cargoId: { type: mongoose.Schema.Types.ObjectId, ref: "Cargo", required: true },
  truckIds: [{ 
    truckId: {type: mongoose.Schema.Types.ObjectId, ref: "Truck", required: true }
  }],
  approved: { type: Boolean ,required: true },
  finished: { type: Boolean ,required: true }
});

module.exports = mongoose.model("Signup", signupSchema);

and controller:

exports.createSignup = (req, res, next) => {
  console.log(req.body.truckIds);
  const signup = new Signup({
    userId: req.userData.userId,
    cargoId: req.body.cargoId,
    truckIds: req.body.truckIds,
    approved: req.body.approved,
    finished: req.body.finished,
    cmrCreated: false
  });
  signup.save()
  .then(result => {
    res.status(201).json({
      message: "Signup created!",
      result: result
    });
  })
  .catch(err => {
    res.status(500).json({
      message: "DB error!"
    });
  });
}

I want to insert data to collection to have something like this:

{
    "_id" : ObjectId("616d536c8e61e369ca82f2e4"),
    "userId" : ObjectId("60868c31caafe530e7e2d04a"),
    "cargoId" : ObjectId("614da0b0812a2f6169163d37"),
    "truckIds" : [id1,id2.....],
    "approved" : false,
    "finished" : false,
    "__v" : 0
}

I don't know how to insert subdocuments(truckIds) into a collection. Thank you in advance.

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!