MongoDB:嵌套数组上的聚合查询

时间:2019-09-30 08:19:51

标签: mongodb nosql mongodb-query

我现在正在研究实时开-高-低-收盘价数据。我想找到每个库存商品的最高价和最低价的时间戳。

{   '_id': ObjectId('5d8db3700a1650f211831f16'),
    'isuLists': [   {   'hgprc': 8100,
                        'isuSrtCd': '000020',
                        'lwprc': 8100,
                        'opnprc': 8100,
                        'trdPrc': 8100},
                    {   'hgprc': 0,
                        'isuSrtCd': '000040',
                        'lwprc': 0,
                        'opnprc': 0,
                        'trdPrc': 500},
                    {   'hgprc': 10050,
                        'isuSrtCd': '000050',
                        'lwprc': 10050,
                        'opnprc': 10050,
                        'trdPrc': 10050},
                     ...
                     similar data continues
                     ...
                    {   'hgprc': 0,
                        'isuSrtCd': '700001',
                        'lwprc': 0,
                        'opnprc': 0,
                        'trdPrc': 9305},
                    {   'hgprc': 3310,
                        'isuSrtCd': '900140',
                        'lwprc': 3310,
                        'opnprc': 3310,
                        'trdPrc': 3310}],
    'trdDd': '20190927',
    'trdTm': '090000'
},
...
{   '_id': ObjectId('5d8d51000a1650f211826c9a'),
    'isuLists': [   {   'hgprc': 8130,
                        'isuSrtCd': '000020',
                        'lwprc': 7880,
                        'opnprc': 8100,
                        'trdPrc': 7980},
                    {   'hgprc': 508,
                        'isuSrtCd': '000040',
                        'lwprc': 498,
                        'opnprc': 504,
                        'trdPrc': 500},
                    {   'hgprc': 10050,
                        'isuSrtCd': '000050',
                        'lwprc': 9720,
                        'opnprc': 10050,
                        'trdPrc': 9780},
                     ...
                     similar data continues
                     ...
                    {   'hgprc': 0,
                        'isuSrtCd': '700001',
                        'lwprc': 0,
                        'opnprc': 0,
                        'trdPrc': 9305},
                    {   'hgprc': 3310,
                        'isuSrtCd': '900140',
                        'lwprc': 3230,
                        'opnprc': 3265,
                        'trdPrc': 3300}],
    'trdDd': '20190927',
    'trdTm': '155959'
}

从9:00到16:00每两秒钟累积一次数据(请看上面的trdTm)。

我设法使用下面的代码找出opnprcclsprc的时间戳,但是找不到最高hgprc和最低lwprc的时间戳。

 [
    {"$unwind": "$isuLists"},
    {"$match": {"isuLists.lwprc": {'$gt': 0}}},
    {
        '$group': {
            '_id': '$isuLists.isuSrtCd',
            'opnTm': {'$first': '$trdTm'},
            'clsTm': {'$last': '$trdTm'}
        }
    }
]

我想找到每个trdTm(发行代码)中最高hgprc和最低lwprc的{​​{1}}(交易时间)。

如何找到以下输出?

isuSrtCd 对于每个问题代码

{issue code(isuSrtCd), timestamp(trdTm) of the highest 'hgprc', timestamp(trdTm) of the lowest 'lwprc'}

感谢您的时间。

2 个答案:

答案 0 :(得分:0)

首先,highPrice和LowPrice可能不是唯一的。这意味着价格可能很高,并且对于许多积累而言可能是相同的。因此它将本质上是数组。

以下是该查询,它实际上为我提供了lowPrice和HighPrice的最小值和最大值,并且还为我提供了timeStamps数组-价格是低还是高。

 db.getCollection('trade').aggregate({"$unwind": "$isuLists"},
   {"$group":{
   "_id":"$isuLists.isuSrtCd",
   "minLowPrice":{$min:"$isuLists.lwprc"},
   "maxHighPrice":{$max:"$isuLists.hgprc"},
   "timeStamp":{"$push": 
   {date:"$trdDd",time:"$trdTm",highPrice:"$isuLists.hgprc",lowPrice:"$isuLists.lwprc"}}
   }},{"$project":
   {
   "sourceCode":"$_id",
   "lowPriceAndTime": { $filter:{input:"$timeStamp",as:"time",cond:{$eq: 
   ["$$time.lowPrice","$minLowPrice"]}}},
   "highPriceAndTime": { $filter:{input:"$timeStamp",as:"time",cond:{$eq: 
   ["$$time.highPrice","$maxHighPrice"]}}}
   }
})

答案 1 :(得分:0)

以下查询可以为我们提供预期的输出:

db.collection.aggregate([
    {
        $unwind:"$isuLists"
    },
    {
        $group:{
            "_id":"$isuLists.isuSrtCd",
            "min_stock":{
                $min:{
                    "lwprc":"$isuLists.lwprc",
                    "trdTm":"$trdTm"
                }
            },
            "max_stock":{
                $max:{
                    "hgprc":"$isuLists.hgprc",
                    "trdTm":"$trdTm"
                }
            }
        }
    },
    {
        $project:{
            "hgTm":"$max_stock.trdTm",
            "lwTm":"$min_stock.trdTm"
        }   
    }
]).pretty()

数据集:

{
    "_id" : ObjectId("5d8db3700a1650f211831f16"),
    "isuLists" : [
        {
            "hgprc" : 8100,
            "isuSrtCd" : "000020",
            "lwprc" : 8100,
            "opnprc" : 8100,
            "trdPrc" : 8100
        },
        {
            "hgprc" : 0,
            "isuSrtCd" : "000040",
            "lwprc" : 0,
            "opnprc" : 0,
            "trdPrc" : 500
        },
        {
            "hgprc" : 10050,
            "isuSrtCd" : "000050",
            "lwprc" : 10050,
            "opnprc" : 10050,
            "trdPrc" : 10050
        },
        {
            "hgprc" : 0,
            "isuSrtCd" : "700001",
            "lwprc" : 0,
            "opnprc" : 0,
            "trdPrc" : 9305
        },
        {
            "hgprc" : 3310,
            "isuSrtCd" : "900140",
            "lwprc" : 3310,
            "opnprc" : 3310,
            "trdPrc" : 3310
        }
    ],
    "trdDd" : "20190927",
    "trdTm" : "090000"
}
{
    "_id" : ObjectId("5d8d51000a1650f211826c9a"),
    "isuLists" : [
        {
            "hgprc" : 8130,
            "isuSrtCd" : "000020",
            "lwprc" : 7880,
            "opnprc" : 8100,
            "trdPrc" : 7980
        },
        {
            "hgprc" : 508,
            "isuSrtCd" : "000040",
            "lwprc" : 498,
            "opnprc" : 504,
            "trdPrc" : 500
        },
        {
            "hgprc" : 10050,
            "isuSrtCd" : "000050",
            "lwprc" : 9720,
            "opnprc" : 10050,
            "trdPrc" : 9780
        },
        {
            "hgprc" : 0,
            "isuSrtCd" : "700001",
            "lwprc" : 0,
            "opnprc" : 0,
            "trdPrc" : 9305
        },
        {
            "hgprc" : 3310,
            "isuSrtCd" : "900140",
            "lwprc" : 3230,
            "opnprc" : 3265,
            "trdPrc" : 3300
        }
    ],
    "trdDd" : "20190927",
    "trdTm" : "155959"
}
{
    "_id" : ObjectId("5d9331cef2e6411a68a145eb"),
    "isuLists" : [
        {
            "hgprc" : 8030,
            "isuSrtCd" : "000020",
            "lwprc" : 10,
            "opnprc" : 8100,
            "trdPrc" : 7980
        },
        {
            "hgprc" : 508,
            "isuSrtCd" : "000040",
            "lwprc" : 498,
            "opnprc" : 504,
            "trdPrc" : 500
        },
        {
            "hgprc" : 10050,
            "isuSrtCd" : "000050",
            "lwprc" : 9720,
            "opnprc" : 10050,
            "trdPrc" : 9780
        },
        {
            "hgprc" : 0,
            "isuSrtCd" : "700001",
            "lwprc" : 0,
            "opnprc" : 0,
            "trdPrc" : 9305
        },
        {
            "hgprc" : 3310,
            "isuSrtCd" : "900140",
            "lwprc" : 3230,
            "opnprc" : 3265,
            "trdPrc" : 3300
        }
    ],
    "trdDd" : "20190927",
    "trdTm" : "155960"
}

输出:

{ "_id" : "900140", "hgTm" : "155960", "lwTm" : "155959" }
{ "_id" : "000020", "hgTm" : "155959", "lwTm" : "155960" }
{ "_id" : "700001", "hgTm" : "155960", "lwTm" : "090000" }
{ "_id" : "000040", "hgTm" : "155960", "lwTm" : "090000" }
{ "_id" : "000050", "hgTm" : "155960", "lwTm" : "155959" }