1

I have a schema exported like that:

const PackageSchema = new Schema({
  name: { type: String, required: true },
  maneuver: [
    {
      maneuverId: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: ManeuverMainly,
      },
      period: { type: String, enum: ["day", "night"], required: true },
    },
  ],
  timestamp: { type: Date, default: Date.now() },
});

When I make a find() like that:

Package.find().populate("maneuver", "name").exec((err, data) => {
    if (err) {
      res.status(500).send({ message: "Failed!" });
      return;
    }
    res.status(200).send(data);
});

My populate method does not work. How can I populate my every maneuverId from PackageSchema with my name column from ManeuverMainlySchema?

Obs: my ManeuverMainlySchema bellow:

const ManeuverMainlySchema = new Schema({
  name: { type: String, required: true },
  description: { type: String, required: true },
  timestamp: { type: Date, default: Date().now },
});
2
  • not tested, but have you tried .populate("maneuver. maneuverId", "name")? taken from stackoverflow.com/questions/16641210/… Commented Dec 23, 2021 at 13:39
  • ill add an answer then for future people to see Commented Dec 23, 2021 at 13:46

1 Answer 1

1

taken from Mongoose populate with array of objects containing ref you have to specify the field within the object of the array you want to populate against.

Package.find().populate("maneuver.maneuverId", "name").exec((err, data) => {
    if (err) {
      res.status(500).send({ message: "Failed!" });
      return;
    }
    res.status(200).send(data);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.