1

我有一个存储在 mongodb 中的 JSON 文档,其中一个子文档是日期时间戳。我需要查询和过滤日期范围之间的子文档。

我正在使用 mongo shell 来处理查询。

{
    "_id": ObjectID("5d9bf09c242af456ff5dd149"),
    "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"
                }
            }
        ]
    },
}

我正在寻找一个查询,该查询必须仅返回日期 2019-10-06T02:12:44Z 和 2019-10-08T02:12:44Z 之间的嵌入文档子集。

4

1 回答 1

1

我们需要将子文档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"
                }
            }
        ]
    }
}
于 2019-10-08T04:53:02.240 回答