【发布时间】:2020-05-05 01:12:27
【问题描述】:
您能否确认正确的查询以列出 20 个最常用的主题标签以及它们在称为“推文”的 MongoDB 推文集合中使用的数字计数?
集合中的每个文档都代表一条推文。
请找到其中一个 JSON 格式的文档(推文)here
我尝试了以下查询:
db.tweets.aggregate([
{
$unwind: "$entities.hashtags"},
{"$group" : {_id:"$entities.hashtags", count:{$sum:1}}},
{ $sort : { count : -1 } },
{ $limit : 20 }
])
展开用于分隔具有多个主题标签的文档。
输出看起来很接近:
/* 1 */
{
"_id" : {
"text" : "PrevenciónEsSalud",
"indices" : [
0,
18
]
},
"count" : 118.0
}
/* 2 */
{
"_id" : {
"text" : "DYK",
"indices" : [
0,
4
]
},
"count" : 112.0
}
/* 3 */
{
"_id" : {
"text" : "ActivadosPorLaSalud",
"indices" : [
0,
20
]
},
"count" : 45.0
}
/* 4 */
{
"_id" : {
"text" : "COVID19",
"indices" : [
15,
23
]
},
"count" : 43.0
}
/* 5 */
{
"_id" : {
"text" : "HelloMyNameIs",
"indices" : [
9,
23
]
},
"count" : 41.0
}
/* 6 */
{
"_id" : {
"text" : "Quito",
"indices" : [
15,
21
]
},
"count" : 40.0
}
/* 7 */
{
"_id" : {
"text" : "LoMásLeído",
"indices" : [
20,
31
]
},
"count" : 40.0
}
/* 8 */
{
"_id" : {
"text" : "COVID19",
"indices" : [
18,
26
]
},
"count" : 39.0
}
/* 9 */
{
"_id" : {
"text" : "COVID19",
"indices" : [
0,
8
]
},
"count" : 38.0
}
/* 10 */
{
"_id" : {
"text" : "PrevenciónGripe",
"indices" : [
0,
16
]
},
"count" : 37.0
}
/* 11 */
{
"_id" : {
"text" : "COVID19",
"indices" : [
21,
29
]
},
"count" : 36.0
}
/* 12 */
{
"_id" : {
"text" : "COVID19",
"indices" : [
128,
136
]
},
"count" : 36.0
}
/* 13 */
{
"_id" : {
"text" : "COVID19",
"indices" : [
40,
48
]
},
"count" : 35.0
}
/* 14 */
{
"_id" : {
"text" : "QuédateEnCasa",
"indices" : [
0,
14
]
},
"count" : 35.0
}
/* 15 */
{
"_id" : {
"text" : "ICYMI",
"indices" : [
0,
6
]
},
"count" : 35.0
}
/* 16 */
{
"_id" : {
"text" : "NosCuidamosTodos",
"indices" : [
0,
17
]
},
"count" : 34.0
}
/* 17 */
{
"_id" : {
"text" : "JuntosEcuador",
"indices" : [
0,
14
]
},
"count" : 34.0
}
/* 18 */
{
"_id" : {
"text" : "COVID19",
"indices" : [
24,
32
]
},
"count" : 31.0
}
/* 19 */
{
"_id" : {
"text" : "EsteVirusLoParamosUnidos",
"indices" : [
0,
25
]
},
"count" : 28.0
}
/* 20 */
{
"_id" : {
"text" : "COVID19",
"indices" : [
23,
31
]
},
"count" : 28.0
}
但是,期望的结果是为 hastag 设置一列,为计数设置另一列,仅用于前 20 个重复次数最多的主题标签。
感谢您帮助我们获取此集合中最常用的 20 个主题标签。
谢谢。
【问题讨论】:
-
the desired outcome is to have a column for the hastag是什么意思? -
我认为你应该在
$entities.hashtags.text上分组。
标签: mongodb mongodb-query