#node.js #mongodb #mongoose
#node.js #mongodb #мангуст
Вопрос:
Как заполнить массив элементов полным объектом?
Это то, что я пытаюсь сделать:
const documentCollection = await DocumentCollection.find({})
.populate({ path: 'items', populate: { path: 'documents', model: 'Document' } });
Но items
поля пусты documentCollection
. почему? не уверен, чего мне здесь не хватает
Вот модель мангуста:
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' } }
)
);
Ответ №1:
Попробуйте это:
const documentCollection = await DocumentCollection.find({})
.populate('items.documents');
Ответ №2:
Я думаю, вы пропустили «s» documents
.
const documentCollection = await DocumentCollection.find({})
.populate({ path: 'items', populate: { path: 'documents', model: 'Document' } });
Комментарии:
1. Я отредактировал свой ответ, я думаю, вы пропустили s в
path: 'documents'
2. все еще произошло.