I have below type of data:
{
"_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"
}
]
}
Below conditions needs to apply:
- startDate $gte ISODate('2018-01-15T11:13:14.000Z') & endDate $lte ISODate('2018-02-12T11:13:14.000Z')
need to group them based on below category
PremChange>10- records will in one set with limit
APTCChange>10- records will in one set with limit MbrRespChg>10- records will in one set with limit
Expected result:
{
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"
}
please suggest me how i need to write mongodb query, i wrote a
query using match, unwind and project but i am not getting desire
output.