MongoDB聚合推文 - 不区分大小写

时间:2016-11-03 14:47:21

标签: mongodb twitter mongodb-query aggregation-framework

我在MongoDB数据库中有推文。结构:

{
    "_id" : ObjectId("111111111111111111"),
    //...
    "entities" : {
        "hashtags" : [ 
            {
                "text" : "HASTAG",
                "indices" : [ 
                    25, 
                    33
                ]
            }
        ],
    },
}
{
    "_id" : ObjectId("222222222222222222222"),
    //...
    "entities" : {
        "hashtags" : [ 
            {
                "text" : "hashtag",
                "indices" : [ 
                    25, 
                    33
                ]
            }
        ],
    },
}

如何按hastgas分组而不是区分大小写的主题标签?

db.getCollection('tweets').aggregate(
    { '$unwind': '$entities.hashtags'},
    { '$group': { '_id': '$entities.hashtags.text', 'tagCount' : {'$sum' : 1} } } ,
    { '$sort': {'tagCount': -1 } } 
)

这个例子没问题,萌芽区分大小写。如何使它不区分大小写?

由于

1 个答案:

答案 0 :(得分:1)

我们想的太多了,但解决方案有时非常容易和小。 "_id": { "$toLower": "$entities.hashtags.text" }