Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写mongodb查询以在不同的时间获取每个msg_名称的消息数_Mongodb_Mongodb Query - Fatal编程技术网

如何编写mongodb查询以在不同的时间获取每个msg_名称的消息数

如何编写mongodb查询以在不同的时间获取每个msg_名称的消息数,mongodb,mongodb-query,Mongodb,Mongodb Query,我已将多个文本案例的所有信息传递给mongodb 我试图通过从mongodb获取数据,在matplotlib中绘制图形。 我需要查询数据库以获取每个消息名称在不同时间的消息数 These are documents in message collection: { _id : 1 msg_name : "A" time : "10" } { _id : 2

我已将多个文本案例的所有信息传递给mongodb 我试图通过从mongodb获取数据,在matplotlib中绘制图形。 我需要查询数据库以获取每个消息名称在不同时间的消息数

These are documents in message collection:
    {   
        _id : 1
        msg_name : "A"
        time : "10"
    }
    {   
        _id : 2
        msg_name : "A"
        time : "20"
    }
    {   
        _id : 3
        msg_name : "B"
        time : "20"
    }
    {   
        _id : 4
        msg_name : "C"
        time : "30"
    }
    {   
        _id : 5
        msg_name : "B"
        time : "30"
    }
    
    query to get distinct times:
    db.msg_coll.distinct("time")
    
    output: [10,20,30]
    
    query to group message names with time:
    db.msg_coll.aggregate([{ $group : { _id : "$msg_name" , time_list : { $push : "$time" }}}])
    
    output : 
    { _id : "A", time_list : [10,20 ] }
    { _id : "B", time_list : [20,30 ] }
    { _id : "C", time_list : [30 ] }
    
    Now, I need to write the query to get number of messages for each msg_name at distinct times.
    time_axis = [10,20,30]
    for msg "A", there are two messages at time 10 and 20  and no message at time 30,
    so result is [1,1,0]
    Expected result:
    
    { _id : "A", no_of_msgs = [1,1,0] }
    { _id : "B", no_of_msgs = [0,1,1] }
    { _id : "C", no_of_msgs = [0,0,1] }