0

I'm fairly new to MongoDB and Mongoose and I am trying to populate a nested array without any luck.

Users have a prop called 'proceedings' which is an array of objects. Each element in that array contains some props including and array called 'forms'.

proceedings[i].forms holds refs to all the forms in the forms collection for the proceeding.

There is nothing to populate in 'proceedings' other than 'forms'. I am trying to populate the nested 'forms' array for each proceeding.

So:

User: {
  _id: ObjectId,
  proceedings: [
    {
      _id: ObjectId,
      forms: [
        ObjectId,
        ObjectId,
        ObjectId
      ]
    },
    {
      _id: ObjectId,
      forms: [
        ObjectId,
        ObjectId,
        ObjectId
      ]
    }
  ]
}

Schemas:

const formSchema = new Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  },
  values: { type: mongoose.Schema.Types.Mixed }
}, { timestamps: true });

const proceedingsSchema = new Schema({
  initial_questions: {},
  forms: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Form'
    }
  ]
});

const userSchema = new Schema({
  email: {
    type: String,
    default: '',
    unique: true
  },
  password: {
    type: String,
    default: ''
  },
  sign_up_date: {
    type: Object,
    default: moment(),
    get: parseDate
  },
  proceedings: [proceedingsSchema]
}, { timestamps: true });

userSchema.set('toObject', { getters: true });
userSchema.set('toJSON', { getters: true });

export const User = mongoose.model('Users', userSchema);
export const Form = mongoose.model('Forms', formSchema);

Query:

User.findOne({ _id: id })
  .populate({
    path: 'proceedings',
    populate: {
      path: 'forms',
      model: 'Form'
    }
  })
  .exec((err, user) => {
    console.log(user.proceedings[0].forms)
  })

The result is an array of ObjectId's instead of the populated forms in their place.

Any help greatly appreciated.

Mongoose - v5.0.1

2
  • you never define your proceedingsSchema model Commented Jan 25, 2018 at 21:44
  • @dprogramz Thanks for having a look. The proceedings are just a prop on the User Schema. The forms are written to heavily so I moved them to their own collection while the proceedings are really only written to a couple times other then adding a ref to another form. Commented Jan 26, 2018 at 11:49

1 Answer 1

2

After thinking more about @dprogramz comment I decided to move the proceedings to their own collection. This query now works:

 Proceeding.find({ userId: id })
    .populate({
      path: 'forms',
      model: 'Forms'
    })
    .exec((err, proceedings) => {
      if (err) {
        logger.error(err.message);
        return res.send({ error: err.message });
      }

      if (!proceedings) {
        return res.send({ error: 'No such proceedings' });
      }

      return res.send({ ok: { proceedings, proceedingId: proceedings[proceedings.length - 1]._id }});
    });
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.