如何在mongo中查询日期范围,其中字段本身是日期

问题描述 投票:0回答:1

我在mongodb中存储了一个JSON文档,其中子文档为date-time-stamp。我需要查询和过滤日期范围之间的子文档。

我正在使用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之间必须返回嵌入式文档子集的查询。

mongodb mongodb-query mongo-shell range-query
1个回答
0
投票

我们需要将子文档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"
                }
            }
        ]
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.