Нам нужно преобразовать поддокумент report
в массив пар ключ-значение, где ключ будет представлять дату. Позже этот массив должен быть отфильтрован и снова преобразован в объект.
Следующий запрос может дать нам ожидаемый результат:
db.collection.aggregate([
{
$addFields:{
"report":{
$objectToArray:"$report"
}
}
},
{
$addFields:{
"report":{
$filter:{
"input":"$report",
"as":"doc",
"cond":{
$and:[
{
$gte:["$$doc.k","2019-10-06T02:12:44Z"]
},
{
$lte:["$$doc.k","2019-10-08T02:12:44Z"]
}
]
}
}
}
}
},
{
$addFields:{
"report":{
$arrayToObject:"$report"
}
}
}
]).pretty()
Набор данных:
{
"_id" : ObjectId("5d9c15312cb9ef5d628ea95d"),
"configId" : "c2",
"name" : "ajit test",
"description" : "this is test desc",
"confidence" : 0,
"report" : {
"2019-10-05T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "2.1",
"high" : "4.0"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "4.5",
"high" : "4.7"
}
}
],
"2019-10-06T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "2.1",
"high" : "4.0"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "4.5",
"high" : "4.7"
}
}
],
"2019-10-08T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
}
]
}
}
Выход:
{
"_id" : ObjectId("5d9c15312cb9ef5d628ea95d"),
"configId" : "c2",
"name" : "ajit test",
"description" : "this is test desc",
"confidence" : 0,
"report" : {
"2019-10-06T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "2.1",
"high" : "4.0"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "4.5",
"high" : "4.7"
}
}
],
"2019-10-08T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
}
]
}
}