需要在 mongodb 中使用自定义结果填充块中的期望结果

Need desired outcome in populate block with customized result in mongodb

我有四个合集 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 中使用 mongoose 库

 // 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'
          }

更新:

您可以使用虚拟将银行详细信息填充到新字段中。像

OtherDetailsSchema.virtual('custom_bank_details', {
  ref: 'BankDetails',
  localField: 'bank_details',
  foreignField: '_id',
  justOne: true
});

Person.findById(id).populate({
    path: 'other_details',
    populate: [
      {path: 'occupation_details'},
      {path: 'custom_bank_details'}
]})

原创

我不是 mongoose 用户,所以我不确定是否可以填充新的字段名称。如果可行,您可以很容易地使用带查找的聚合来实现。

类似

person.aggregate([
  {"$lookup":{
    "from":"other_details",
    "let":{"other_details":"$other_details"},
    "pipeline":[
      {"$match":{"$expr":{"$eq":["$id","$$other_details"]}}},
      {"$lookup":{
        "from":"occupation_details",
        "localField":"occupation_details",
        "foreignField":"id",
        "as":"occupation_details"
      }},
      {"$unwind":"$occupation_details"},
      {"$lookup":{
        "from":"bank_details",
        "localField":"bank_details",
        "foreignField":"id",
        "as":"custom_bank_details"
      }},
      {"$unwind":"$custom_bank_details"},
    ],
    "as":"other_details"
   }},
   {"$unwind":"$other_details"}
 ])

工作示例https://mongoplayground.net/p/5Kee0tBQmTd