0

I have a schema like this

const Schema1 = new Schema({
  field11: String,
  field12: [
             {  _id: Schema.Types.ObjectId, 
                title: String
             }
           ]
})

and another schema which has a field to reference to filed of the first collection as below

const Schema2 = new Schema({
  field21: String,
  field22: [
             {_id: {type: Schema.Types.ObjectId}, 
             {ref: 'Schema1.filed12'}
           ]
})

I need to populate field22 in schema2. How do I need to do it.

The below query doesn't work for me.

Schema2.find(field21).populate('Schema1.field12')
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317

1 Answers1

0

According to the documentation:

The ref option is what tells Mongoose which model to use during population, in our case the Story model. All _ids we store here must be document _ids from the Story model.

You are attempting to store non-id fields as a reference that should point to subdocuments that are nested in an array of your Schema1 model. This simply doesn't work.

dnickless
  • 10,733
  • 1
  • 19
  • 34