如何基于某些特定条件检索深度嵌套的数组

时间:2018-10-02 16:39:09

标签: java spring mongodb

我有一个称为abc的集合(让我们假设),在这个集合中,我有多个结构如下的文档:

{
   "customerId":"Id",
   "contract":[
      {
         "contractId":"con1",
         "contractName":"conName1",
         "pricing":[
            {
               "pricingName":"priceName1",
               "billProfile":[
                  {
                     "billCode":"code1",
                     "billName":"Name1"
                  },
                  {
                     "billCode":"code2",
                     "billName":"Name2"
                  }
               ]
            },
            {
               "pricingName":"priceName2",
               "billProfile":[
                  {
                     "billCode":"code3",
                     "billName":"Name3"
                  }
               ]
            }
         ]
      },
      {
         "contractId":"con2",
         "contractName":"conName2",
         "pricing":[
            {
               "pricingName":"priceName3",
               "billProfile":[
                  {
                     "billCode":"code4",
                     "billName":"Name4"
                  },
                  {
                     "billCode":"code5",
                     "billName":"Name5"
                  }
               ]
            },
            {
               "pricingName":"priceName4",
               "billProfile":[
                  {
                     "billCode":"code6",
                     "billName":"Name6"
                  }
               ]
            }
         ]
      }
   ]
}

对于这样的多个文档,我想搜索特定的账单代码,然后返回该账单代码的相应的billProfile,定价和合同详细信息。例如,如果我搜索billCode code5,则数据库的相应输出应为:

{
   "customerId":"Id",
   "contract":[
      {
         "contractId":"con2",
         "contractName":"conName2",
         "pricing":[
            {
               "pricingName":"priceName3",
               "billProfile":[
                  {
                     "billCode":"code5",
                     "billName":"Name5"
                  }
               ]
            }
         ]
      }
   ]
}

到目前为止,我已经尝试过:

使用位置运算符$,但是只能将其用于文档的更深一层。尝试检索具有billCode但定价和票据配置文件不正确的特定合同

我知道我们可以为这个事情使用聚合,但是我不知道如何在这种复杂的检索上执行聚合。我需要在java spring项目中使用aggreagtion从数据库中获取数据,然后将其存储在类模型中。知道如何对该数据集执行汇总吗?

1 个答案:

答案 0 :(得分:0)

您可以同时使用$filter$map

注意:我尚未使用Java进行过测试。但它在MongoDB GUI

中起作用
db.getCollection('customer').aggregate([ 
{ "$project": {
    "_id":1,
    "customerId" :1,
    "contract": {
      "$filter": {
        "input": {
          "$map": {
            "input": "$contract",
            "as": "con",
            "in": {
              "pricing": {
                "$filter": {
                 "input": {
                    "$map": {
                  "input": "$$con.pricing",
                  "as": "pric",
                   "in" : {
                     "billProfile" : {
                       "$filter" : {
                         "input" : "$$pric.billProfile",
                          "as" : "billProf",
                          "cond": {
                                "$eq": [ "$$billProf.billCode", "code5" ] 
                            }

                       }
                     }
                   }
                }
                 },
                 "as": "pric",
                  "cond": {
                    "$and": [
                      { "$gt": [ { "$size": "$$pric.billProfile" }, 0 ] }
                    ]
                  }
                }
              }             
            }
          }
       },
       "as": "con",
        "cond": {
          "$and": [
            { "$gt": [ { "$size": "$$con.pricing" }, 0 ] }
          ]
        }
      }
     }
    }
  }
]
)