从Map创建Flatnedned Object(降低View查询的复杂性) - Mongo DB

时间:2018-03-19 13:34:53

标签: mongodb view aggregation-framework

我必须为Mongo DB创建一个视图。我提出了类似下面的内容: -

db.createView("weNeed","Master",
 [
{$project:
     {
         _id:"$_id",
         documents:{
            $concatArrays:[
             {$ifNull:
             [{$map:{
                input:"$documents.COMPLETED",
                 as:"document",
                 in:{
                     entity:"$name",
                     status:"COMPLETED",
                     name:"$$document.name",
                     category:"$$document.category",
                     description:"$$document.description",
                     submittedDate:"$$document.submittedDate",
                     expirationDate:"$$document.expirationDate"

                     }
             }},[]]},
             {$ifNull:
             [{$map:{
                input:"$documents.REQUIRED",
                 as:"document",
                 in:{ entity:"$name",
                     status:"REQUIRED",
                     name:"$$document.name",
                     category:"$$document.category",
                     description:"$$document.description",
                     submittedDate:"$$document.submittedDate",
                     expirationDate:"$$document.expirationDate"
                     }
             }},[]]},
             {$ifNull:
             [{$map:{
                input:"$documents.DEFERRED",
                 as:"document",
                 in:{ entity:"$name",
                     status:"DEFERRED",
                     name:"$$document.name",
                     category:"$$document.category",
                     description:"$$document.description",
                     submittedDate:"$$document.submittedDate",
                     expirationDate:"$$document.expirationDate"
                     }
             }},[]]}
             ]
         }
     }
     }
     ]
     )

documents是一个类似下面的对象: -

 "COMPLETED" : [ 
            {
                "externalIds" : {
                    "fenergo" : "35"
                },
                "name" : "CRS Self Certification",
                "category" : "Tax",
                "description" : "CRS Self Certification",
                "submittedDate" : ISODate("2017-12-05T18:48:26.183Z"),
            }
}],
"UNRELATED" : [ 
            {
                "externalIds" : {
                    "fenergo" : "35"
                },
                "name" : "CRS Self Certification",
                "category" : "Tax",
                "description" : "CRS Self Certification",
                "submittedDate" : ISODate("2017-12-05T18:48:26.183Z"),
            }]

遗憾的是,我不得不重复地图代码为每个状态提取文档,然后为返回的数组中的每个Object创建展平的Object。

想要输出如下: -

 "documents" : [ 
                {
                   "status":"COMPLETED",
                    "name" : "CRS Self Certification",
                    "category" : "Tax",
                    "description" : "CRS Self Certification",
                    "submittedDate" : ISODate("2017-12-05T18:48:26.183Z"),
                },
                {
                   "status":"UNRELATED",
                    "name" : "CRS Self Certification",
                    "category" : "Tax",
                    "description" : "CRS Self Certification",
                    "submittedDate" : ISODate("2017-12-05T18:48:26.183Z"),
                }

有什么方法我不需要为我的对象的所有键重复它?我认为如果明天我有另一种身份,这将是一个非常糟糕的方法。

请帮忙!

1 个答案:

答案 0 :(得分:1)

您可以在3.6中将项目阶段重写为以下内容。

  {
  "$project":{
    "documents":{
      "$reduce":{
        "input":{"$objectToArray":"$documents"},
        "initialValue":[],
        "in":{"$concatArrays":["$$value", {"$map":{ input:"$$this.v",  as:"mvalue", in:{"$mergeObjects":[{"status":"$$this.k"}, "$$mvalue"]} }}]}
      }
    }
  }
}