MongoDB查询 - 使用组和项目展开并匹配和限制

时间:2018-02-15 19:47:10

标签: json mongodb aggregation-framework

我有以下类型的数据:

    {
        "_id" : "HCCIDM1234567A",
        "RecordT" : "THISAPTC",
        "history" : [ 
    {
        "startDate" : ISODate("2018-01-14T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-09T11:13:14.000Z"),
        "APTCChange" : 1200,"PremChange" : 1300,"MbrRespChg" : 100,
        "NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-15T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-10T11:13:14.000Z"),
        "APTCChange" : 16,"PremChange" : 0,"MbrRespChg" : -200,
        "NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-16T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-08T11:13:14.000Z"),
        "APTCChange" : -16,"PremChange" : 0,
        "MbrRespChg" : -224,"NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-17T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-07T11:13:14.000Z"),
        "APTCChange" : 0,"PremChange" : 15,
        "MbrRespChg" : -224,"NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-18T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-06T11:13:14.000Z"),
        "APTCChange" : 0,"PremChange" : -15,
        "MbrRespChg" : -70,"NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-19T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-05T11:13:14.000Z"),
        "APTCChange" : -10,"PremChange" : -15,
        "MbrRespChg" : -77, "NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-20T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-04T11:13:14.000Z"),
        "APTCChange" : 0,"PremChange" : 1,
        "MbrRespChg" : 77,"NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-12T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-23T11:13:14.000Z"),
        "PremChange" : 0,"APTCChange" : 16,
        "MbrRespChg" : -200,"NPN" : "U65"
    }
]
},
{
"_id" : "HCCIDM1234567B",
"RecordT" : "THISAPTC",
"history" : [ 
    {
        "startDate" : ISODate("2018-01-14T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-09T11:13:14.000Z"),
        "APTCChange" : 1200,
        "PremChange" : 1300,
        "MbrRespChg" : 100,
        "NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-15T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-10T11:13:14.000Z"),
        "APTCChange" : 16,
        "PremChange" : 0,
        "MbrRespChg" : -200,
        "NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-16T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-08T11:13:14.000Z"),
        "APTCChange" : -16,
        "PremChange" : 0,
        "MbrRespChg" : -224,
        "NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-17T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-07T11:13:14.000Z"),
        "APTCChange" : 0,
        "PremChange" : 15,
        "MbrRespChg" : -224,
        "NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-18T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-06T11:13:14.000Z"),
        "APTCChange" : 0,
        "PremChange" : -15,
        "MbrRespChg" : -70,
        "NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-19T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-05T11:13:14.000Z"),
        "APTCChange" : -10,
        "PremChange" : -15,
        "MbrRespChg" : -77,
        "NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-20T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-04T11:13:14.000Z"),
        "APTCChange" : 0,
        "PremChange" : 1,
        "MbrRespChg" : 77,
        "NPN" : "U65"
    }, 
    {
        "startDate" : ISODate("2018-01-12T11:13:14.000Z"),
        "endDate" : ISODate("2018-02-23T11:13:14.000Z"),
        "PremChange" : 0,
        "APTCChange" : 16,
        "MbrRespChg" : -200,
        "NPN" : "U65"
    }
]
}, 
{
    "_id" : "HCCIDM1234567C",
    "RecordT" : "THISAPTC",
    "history" : [ 
        {
            "startDate" : ISODate("2018-01-14T11:13:14.000Z"),
            "endDate" : ISODate("2018-02-09T11:13:14.000Z"),
            "APTCChange" : 1200,
            "PremChange" : 1300,
            "MbrRespChg" : 100,
            "NPN" : "U65"
        }, 
        {
            "startDate" : ISODate("2018-01-15T11:13:14.000Z"),
            "endDate" : ISODate("2018-02-10T11:13:14.000Z"),
            "APTCChange" : 16,
            "PremChange" : 0,
            "MbrRespChg" : -200,
            "NPN" : "U65"
        }, 
        {
            "startDate" : ISODate("2018-01-16T11:13:14.000Z"),
            "endDate" : ISODate("2018-02-08T11:13:14.000Z"),
            "APTCChange" : -16,
            "PremChange" : 0,
            "MbrRespChg" : -224,
            "NPN" : "U65"
        }, 
        {
            "startDate" : ISODate("2018-01-17T11:13:14.000Z"),
            "endDate" : ISODate("2018-02-07T11:13:14.000Z"),
            "APTCChange" : 0,
            "PremChange" : 15,
            "MbrRespChg" : -224,
            "NPN" : "U65"
        }, 
        {
            "startDate" : ISODate("2018-01-18T11:13:14.000Z"),
            "endDate" : ISODate("2018-02-06T11:13:14.000Z"),
            "APTCChange" : 0,
            "PremChange" : -15,
            "MbrRespChg" : -70,
            "NPN" : "U65"
        }, 
        {
            "startDate" : ISODate("2018-01-19T11:13:14.000Z"),
            "endDate" : ISODate("2018-02-05T11:13:14.000Z"),
            "APTCChange" : -10,
            "PremChange" : -15,
            "MbrRespChg" : -77,
            "NPN" : "U65"
        }, 
        {
            "startDate" : ISODate("2018-01-20T11:13:14.000Z"),
            "endDate" : ISODate("2018-02-04T11:13:14.000Z"),
            "APTCChange" : 0,
            "PremChange" : 1,
            "MbrRespChg" : 77,
            "NPN" : "U65"
        }, 
        {
            "startDate" : ISODate("2018-01-12T11:13:14.000Z"),
            "endDate" : ISODate("2018-02-23T11:13:14.000Z"),
            "PremChange" : 0,
            "APTCChange" : 16,
            "MbrRespChg" : -200,
            "NPN" : "U65"
        }
    ]
}

以下条件需要适用:

  1. startDate $ gte ISODate(' 2018-01-15T11:13:14.000Z')& endDate $ lte ISODate(' 2018-02-12T11:13:14.000Z')
  2. 需要根据以下类别对其进行分组

    PremChange> 10-记录将在一组中具有限制
     APTCChange> 10-记录将在一组中有限制  MbrRespChg> 10-记录将在一组中具有限制

  3. 预期结果:

        {
           id: APTCChange,
           "RecordT" : "THISAPTC",
           "hccid':'HCCIDM1234567A',
          'startDate': ISODate("2018-01-14T11:13:14.000Z"),
          "endDate" : ISODate("2018-02-09T11:13:14.000Z"),
          "APTCChange" : 1200,
          "PremChange" : 1300,
          "MbrRespChg" : 100,
          "NPN" : "U65"
         }
         -------- 
         {
           id: APTCChange,
           "RecordT" : "THISAPTC",
           "hccid':'HCCIDM1234567A',
          'startDate': ISODate("2018-01-15T11:13:14.000Z"),
          "endDate" : ISODate("2018-02-08T11:13:14.000Z"),
          "APTCChange" : 16,
          "PremChange" : 0,
          "MbrRespChg" : -200,
           "NPN" : "U65"
         }
         ---------
         {
           id: PremChange,
          "RecordT" : "THISAPTC",
           "hccid':'HCCIDM1234567A',
          'startDate': ISODate("2018-01-16T11:13:14.000Z"),
          "endDate" : ISODate("2018-02-07T11:13:14.000Z"),
          "APTCChange" : 0,
          "PremChange" : 15,
          "MbrRespChg" : -224,
          "NPN" : "U65"
         } 
        -----
       {
           id: MbrRespChg,
           "RecordT" : "THISAPTC",
           "hccid':'HCCIDM1234567A',
          'startDate': ISODate("2018-01-18T11:13:14.000Z"),
          "endDate" : ISODate("2018-02-04T11:13:14.000Z"),
          "APTCChange" : 0,
          "PremChange" : 1,
          "MbrRespChg" : 77,
          "NPN" : "U65"
         }
         ---
         {
           id: APTCChange,
           "RecordT" : "THISAPTC",
           "hccid':'HCCIDM1234567B',
          'startDate': ISODate("2018-01-14T11:13:14.000Z"),
          "endDate" : ISODate("2018-02-09T11:13:14.000Z"),
          "APTCChange" : 1200,
          "PremChange" : 1300,
          "MbrRespChg" : 100,
          "NPN" : "U65"
         }
         -------- 
         {
           id: APTCChange,
           "RecordT" : "THISAPTC",
           "hccid':'HCCIDM1234567B',
          'startDate': ISODate("2018-01-15T11:13:14.000Z"),
          "endDate" : ISODate("2018-02-08T11:13:14.000Z"),
          "APTCChange" : 16,
          "PremChange" : 0,
          "MbrRespChg" : -200,
           "NPN" : "U65"
         }
         ---------
         {
           id: PremChange,
          "RecordT" : "THISAPTC",
           "hccid':'HCCIDM1234567B',
          'startDate': ISODate("2018-01-16T11:13:14.000Z"),
          "endDate" : ISODate("2018-02-07T11:13:14.000Z"),
          "APTCChange" : 0,
          "PremChange" : 15,
          "MbrRespChg" : -224,
          "NPN" : "U65"
         } 
        -----
       {
           id: MbrRespChg,
           "RecordT" : "THISAPTC",
           "hccid':'HCCIDM1234567B',
          'startDate': ISODate("2018-01-18T11:13:14.000Z"),
          "endDate" : ISODate("2018-02-04T11:13:14.000Z"),
          "APTCChange" : 0,
          "PremChange" : 1,
          "MbrRespChg" : 77,
          "NPN" : "U65"
         }
    

    请建议我如何编写mongodb查询,我写了一个           查询使用匹配,展开和项目,但我没有得到欲望           输出

1 个答案:

答案 0 :(得分:0)

您可以尝试以下聚合。不确定为什么你需要在这里分组。

$match将历史数组过滤到至少一个元素在指定日期范围内的位置。

$unwind展平历史数组。

$match过滤历史文档,其中元素与指定的日期范围匹配。

$match过滤关键值大于10的历史文档。

db.col.aggregate([
  {"$match":{
  "history":{
    "$elemMatch":{
      "startDate":{"$gte":ISODate("2018-01-15T11:13:14.000Z")},
      "endDate":{"$lte":ISODate("2018-02-12T11:13:14.000Z")}
     }
   }
 }},
 {"$unwind":"$history"},
 {"$match":{
   "history.startDate":{"$gte":ISODate("2018-01-15T11:13:14.000Z")}, 
   "history.endDate":{"$lte":ISODate("2018-02-12T11:13:14.000Z")}
 }},
 {"$match":{
   "$or":[
     {"history.APTCChange":{"$gt":10}},
     {"history.PremChange":{"$gt":10}},
     {"history.MbrRespChg":{"$gt":10}}
    ]
 }}
])