0

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' } }
  )
);

2 Answers 2

1

Try this:

const documentCollection = await DocumentCollection.find({})
    .populate('items.documents');
Sign up to request clarification or add additional context in comments.

Comments

0

I think you missed an 's' in documents.

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

2 Comments

I have edited my answer, I think you missed an s in path: 'documents'
still happened.

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.