简体   繁体   中英

mongoose populate array return empty results

How to populate the items array with full object?

This is what I try to do:

const documentCollection = await DocumentCollection.find({})
    .populate({ path: 'items', populate: { path: 'documents', model: 'Document' } });

But items fields is empty in documentCollection . why? not sure what I missing here

Here is the mongoose model:

export const DocumentsCollection = mongoose.model('Document-Collection',
  new mongoose.Schema({
    name: { type: String },
    items: [
      {
        name: { type: String },
        documents: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Document' }],
      },
    ],
  })
);

export const Document = mongoose.model( 'Document',
  new mongoose.Schema(
    {
      name: { type: String },
      description: { type: String }
    },
    { timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } }
  )
);

Try this:

const documentCollection = await DocumentCollection.find({})
    .populate('items.documents');

I think you missed an 's' in documents .

const documentCollection = await DocumentCollection.find({})
    .populate({ path: 'items', populate: { path: 'documents', model: 'Document' } });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM