MongoDB - 从列表中获取@DBRef值作为单独的对象

时间:2017-12-05 08:35:54

标签: mongodb spring-mvc

这是当前对象

{
"chargeCodeIds" : [
    1,
    2
],
"customChargeCode" : [
    {
        "_id" : 1,
        "chargeCodeType" : "Department",
        "name" : "IT",
        "isBillable" : true,
    },
    {
        "_id" : 2,
        "chargeCodeType" : "Task"
        "name" : "Development",
        "isBillable" : true
    }
]

我们有查询从他们的收集中搜索收费代码说" customChargeCode"并将它们列在一个对象" customChargeCode"作为对象的数组。但我不想要它在阵列中。

以下是当前查询:

    db.authorizeChargeAssociation.aggregate([
{
    $project: {
        chargeCodeIds: {
          $map: {
             input: {
                  $map: {
                      input:"$chargeCodes",
                      in: {
                           $arrayElemAt: [{$objectToArray: "$$this"}, 1]
                      },
                  }
             },
             in: "$$this.v"
           }
        },
     }
},

{
    $lookup: {

       from: "customChargeCode",
        localField:"chargeCodeIds",
        foreignField:"_id",
        as:"customChargeCode"
    }
},

{$unwind: {path: "$customChargeCodes", preserveNullAndEmptyArrays: true} },

],

{ collation: { locale: "en_US", numericOrdering: true}},
{ allowDiskUse: true}
)

我想要的是获得每个" chargeCodeIds"的单独对象。来自" customChargeCode"收集并将它们命名为" chargeCodeType"值。

预期结果如下:

{
"chargeCodeIds" : [
    1,
    2
],
"Department" : {
        "_id" : 1,
        "chargeCodeType" : "Department",
        "name" : "IT",
        "isBillable" : true,
    },

"Task" :  {
        "_id" : 2,
        "chargeCodeType" : "Task"
        "name" : "Development",
        "isBillable" : true
    }

0 个答案:

没有答案