1

这是我在 tools->command 中运行的命令

{
aggregate : "hashtags",       
pipeline:

[

{$unwind:"$time"},
{$match:{"$time":{$gte:NumberInt(1450854385), $lte:NumberInt(1450854385)}}},
{$group:{"_id":"$word","count":{$sum:1}}}
]

}

这给了我们这个结果

Response from server:
{
   "result": [
     {
       "_id": "dear",
       "count": NumberInt(1) 
    },
     {
       "_id": "ghost",
       "count": NumberInt(1) 
    },
     {
       "_id": "rat",
       "count": NumberInt(1) 
    },
     {
       "_id": "police",
       "count": NumberInt(1) 
    },
     {
       "_id": "bugs",
       "count": NumberInt(3) 
    },
     {
       "_id": "dog",
       "count": NumberInt(2) 
    },
     {
       "_id": "batman",
       "count": NumberInt(9) 
    },
     {
       "_id": "ear",
       "count": NumberInt(1) 
    } 
  ],
   "ok": 1 
}

文档在集合'hashtags'中插入的文档如下图1所示。

{
   "_id": ObjectId("567a483bf0058ed6755ab3de"),
   "hash_count": NumberInt(1),
   "msgids": [
     "1583" 
  ],
   "time": [
     NumberInt(1450854385) 
  ],
   "word": "ghost" 
}

2.

{
   "_id": ObjectId("5679485ff0058ed6755ab3dd"),
   "hash_count": NumberInt(1),
   "msgids": [
     "1563" 
  ],
   "time": [
     NumberInt(1450788886) 
  ],
   "word": "dear" 
}

3.

{
   "_id": ObjectId("567941aaf0058ed6755ab3dc"),
   "hash_count": NumberInt(9),
   "msgids": [
     "1555",
     "1556",
     "1557",
     "1558",
     "1559",
     "1561",
     "1562",
     "1584",
     "1585" 
  ],
   "time": [
     NumberInt(1450787170),
     NumberInt(1450787292),
     NumberInt(1450787307),
     NumberInt(1450787333),
     NumberInt(1450787354),
     NumberInt(1450787526),
     NumberInt(1450787615),
     NumberInt(1450855148),
     NumberInt(1450855155) 
  ],
   "word": "batman" 
}

4.

{
   "_id": ObjectId("567939cdf0058ed6755ab3d9"),
   "hash_count": NumberInt(3),
   "msgids": [
     "1551",
     "1552",
     "1586" 
  ],
   "time": [
     NumberInt(1450785157),
     NumberInt(1450785194),
     NumberInt(1450856188) 
  ],
   "word": "bugs" 
}

所以我想计算两个限制之间的“时间”字段中的值的数量

像这样

foreach word
{
foreach time
{
if((a<time)&&(time<b))
word[count]++
}
}

但我的查询只是给出数组“时间”总大小的输出。

什么是正确的查询?

例如

if lower bound is 1450787615  and upper bound is  1450855155

“时间”中有 3 个值。对于“蝙蝠侠”这个词

The answer should be
{
           "_id": "batman",
           "count": NumberInt(3) 
        },

蝙蝠侠。谢谢。

4

1 回答 1

1

使用以下聚合管道:

db.hashtags.aggregate([
    {
        "$match": {
            "time": {
                "$gte": 1450787615, "$lte": 1450855155  
            }
        }
    },
    { "$unwind": "$time" },
    {
        "$match": {
            "time": {
                "$gte": 1450787615, "$lte": 1450855155  
            }
        }
    },
    {
        "$group": {
            "_id": "$word",
            "count": {
                "$sum": 1
            }
        }
    }
])

对于给定的示例文档,这将产生:

/* 0 */
{
    "result" : [ 
        {
            "_id" : "batman",
            "count" : 3
        }, 
        {
            "_id" : "dear",
            "count" : 1
        }, 
        {
            "_id" : "ghost",
            "count" : 1
        }
    ],
    "ok" : 1
}
于 2015-12-23T09:46:47.630 回答