【发布时间】:2020-11-16 20:59:44
【问题描述】:
我正在尝试用他的才华和才华媒体填充我的用户文档。但我得到了重复的对象。我想将人才作为一个对象,并将媒体与该人才放在一个数组中。
我的用户模型:
{
_id: '5f1acd6e6985114f2c1567ea',
name: 'test user',
email: 'test@email.com'
}
人才模型
{
_id: '5f1acd6e6985114f2c1567fa',
categoryId: '5f1acd6e6985114f2c1567ga',
userId: '5f1acd6e6985114f2c1567ea'
level: '5',
}
人才媒体模式
{
_id: 5f1acd6e6985114f2c156710',
talentId: '5f1acd6e6985114f2c1567fa',
media: 'file.jpg',
fileType: 'image'
}
我有另一个存储类别的模型
{
_id: '5f1acd6e6985114f2c1567ga',
title: 'java'
}
我想要的结果如下
user: {
_id: '',
name: 'test user',
email: 'test@email.com',
talents: [
{
_id: '',
level: '5',
categoryId: {
_id: '',
title: 'java'
},
medias: [
{
_id: '',
file: 'file1.jpg',
fileType: 'image'
},
{
_id: '',
file: 'file2.jpg',
fileType: 'image'
},
]
}
]
}
我还尝试在人才文件中添加人才媒体。但在 MongoDB 文档中找到的大部分不推荐使用。 有这样的人才模型是不是更好,
{
_id: '5f1acd6e6985114f2c1567fa',
categoryId: '5f1acd6e6985114f2c1567ga',
userId: '5f1acd6e6985114f2c1567ea'
level: '5',
medias: [
{
_id: '',
file: 'file1.jpg',
fileType: 'image'
},
{
_id: '',
file: 'file2.jpg',
fileType: 'image'
},
]
}
【问题讨论】: