Я пытаюсь заполнить поле внутри двумерного массива мангустом в NodeJS.
Вот как структурированы мои переводческие данные:
{
"_id" : ObjectId("5cc3fa08c2d98a3ac8e4889a"),
"translation" : [
[
{
"x" : "999",
"y" : "999",
"width" : "555",
"height" : "555",
"idVideo" : ObjectId("5cc401f319bac9285ce0a235")
},
{
"x" : "111",
"y" : "111",
"width" : "666",
"height" : "666",
"idVideo" : ObjectId("5cc401f319bac9285ce0a235")
}
]
],
"__v" : 2
}
TranslationSchema.js
const TranslationSchema = mongoose.Schema(
{
idDocument: {
type: Schema.Types.ObjectId,
ref: 'Document'
},
translation: {
type: [[TranslationCoordinateSchema]],
default: []
}
},
{
strict: true
}
);
TranslationCoordinateSchema.js
const TranslationCoordinateSchema = mongoose.Schema({
x: {
type: String
},
y: {
type: String
},
width: {
type: String
},
height: {
type: String
},
idVideo: {
type: Schema.Types.ObjectId,
ref: 'TranslationVideo'
},
_id: false
});
Я пробовал много вещей, но я не знаю, как структурировать путь, поскольку это двумерный массив.
Для примера я попробовал:
Translation.findById(idTranslation).populate({
path: 'translation.idVideo',
model: 'TranslationVideo'
});
и
Translation.findById(idTranslation).populate({
path: 'translation.translation.idVideo',
model: 'TranslationVideo'
});
и, возможно,
Translation.findById(idTranslation).populate({
path: 'translation..idVideo',
model: 'TranslationVideo'
});
Я ожидаю заполнить idVideo, чтобы я мог вернуть все содержащие данные, но вместо этого у меня есть:
"data": [
{
"type": "translations",
"id": "5cc3fa08c2d98a3ac8e4889a",
"translation": [
[
{
"x": "999",
"y": "999",
"width": "555",
"height": "555",
"idVideo": "5cc401f319bac9285ce0a235"
},
{
"x": "111",
"y": "111",
"width": "666",
"height": "666",
"idVideo": "5cc401f319bac9285ce0a235"
}
]
],
}
]
Решение
Спасибо, Моад Эннаги, за решение.
Я только что отредактировал его решение, чтобы оно работало с ASYNC / AWAIT.
static async findByIdAndPopulateVideos(idTranslation) {
let count;
let arr = [];
if (!(count = await Translation.findById(idTranslation))) {
return;
}
for (let i = 0; i < count.translation.length; i++) {
arr.push(`translation.${i}.idVideo `); // Don't delete the last space !
}
return await Translation.findById(idTranslation).populate({
path: arr.join(''),
model: 'TranslationVideo'
});
}