我有如下所述的四个集合person , other_details, occupation_details, bank_details
person =>[
{
id : 1,
name : 'john',
other_details : 1023
},
{
id : 2,
name : 'mark',
other_details : 99
}
]
other_details => [
{
id: 1023,
married: false,
occupation_details: 144,
bank_details : 10
},
{
id: 99,
married: true,
occupation_details: 45,
bank_details : 11
}
]
occupation_details => [
{
id: 144,
comp_name : 'oscorp inc.'
},
{
id: 45,
comp_name : 'tesla inc.'
}
]
bank_details => [
{
id: 10,
bank : 'Bank of canada'
},
{
id: 11,
bank : 'Peoples bank of canada'
}
]
我正在使用带有nodejs的猫鼬库
// id = 1
person.findById(id).populate({
path: 'other_details',
populate: [
{
path: 'occupation_details'
},
{
path: 'bank_details'
}
]
})
因此上述查询的结果如下=>
{
id : 1,
name : 'john',
other_details : {
id: 1023,
married: false,
occupation_details: {
id: 144,
comp_name : 'oscorp inc.'
},
bank_details : {
id: 10,
bank : 'Bank of canada'
}
}
}
但是由于某些原因,我想要下面的结果
{
id : 1,
name : 'john',
other_details : {
id: 1023,
married: false,
occupation_details: {
id: 144,
comp_name : 'oscorp inc.'
},
bank_details : 10,
custom_bank_details : {
id: 10,
bank : 'Bank of canada'
}
}
}
我需要进行以下更改,bank_details
对象应该以id存在,并且来自其他集合的填充的bank_details
数据应以custom_bank_details
的形式出现在其他对象名称中>
bank_details : 10,
custom_bank_details : {
id: 10,
bank : 'Bank of canada'
}
我有四个集合person,other_details,occupation_details,bank_details如下所述person => [{{id:1,name:'john',other_details:1023},{id:2,name:'mark',...
我不是猫鼬用户,所以我不确定是否可以填充到新的字段名称。如果可行,您可以很容易地通过查找使用聚合。