Home > Software design >  How can i use regex in Mongodb aggregation pipeline in group method?
How can i use regex in Mongodb aggregation pipeline in group method?

Time:09-02

I have a list field called main_keywords in my collection. When i use it like this

pipeline = [
        {'$unwind': '$main_keywords'},
        {'$group': {'_id': '$main_keywords', 'count': {'$sum': 1}}},
        {'$sort': {'count': -1}},
    ]
streamdata = db.objects.aggregate(pipeline)

Results:

{'_id': 'Corona', 'count': 98}
{'_id': 'Korona', 'count': 94}
{'_id': 'Covid', 'count': 93}
{'_id': 'corona', 'count': 91}

But as you can see, I want to show Corona and corona data with regex and the total value in a single field.How can i do ?

CodePudding user response:

You can just add $toLower to your $group stage, like so:

{
    "$group": {
        "_id": {
            $toLower: "$main_keywords"
        },
        "count": {
            "$sum": 1
        }
    }
}

Mongo Playground

  • Related