2

I'm using pymongo version 2.7.2.

I just want to count occurrences by name matching by Hostname.

I don't want to use map/reduce. I want to do it through aggregate function.

Executing this query:

"Counting by name matching by Hostname"

cursor = self.coll.aggregate(
    {"$match": {"HOSTNAME": "XXYYX"}},
    {"$group": {"_id": {"Name": "$NAME"}, "count": {"$sum": 1}}},
)

I got the following error:

line 46, in registerApps
    "count": { "$sum": 1 }
TypeError: aggregate() takes 2 positional arguments but 3 were given

This query is perfectly running over mongodb. But I don't know how to translate it properly to Pymongo.

Could you help me on this?

Thanks very much!

flagg19
  • 1,782
  • 2
  • 22
  • 27

1 Answers1

4

Aggregate takes an array of commands as its parameter:

cursor = self.coll.aggregate(
          [
            { "$match": { "HOSTNAME": "XXYYX" }},
            { "$group": {
                "_id": {"Name": "$NAME"},
                "count": { "$sum": 1 }
            }
          ]
)

as described in the docs

RickyA
  • 15,465
  • 5
  • 71
  • 95