0

I would like to count matching records with pymongo. I have looked up the documentation: https://docs.mongodb.com/manual/reference/operator/aggregation/group/#group-by-month-day-and-year

I understand that I should do something like:

db.foo_baz.aggregate([{'$group': {'_id': '$baz_id', 'count': {'$sum': 1}}}])

However, I am getting this error:

      File "/usr/lib/python3.6/site-packages/mongomock/collection.py", line 1531, in aggregate
    from_field = key.replace('$', '')
AttributeError: 'int' object has no attribute 'replace'

Add Comment

I would think that pymongo would evaluate it to this mongodb statement:

 db.foo_baz.aggregate({{$group: {_id: '$baz_id', count: {$sum: 1}}}])

Which gives me the expected result when ran from mongodb console, which is: {"_id" : NumberInt("1234"), "count" : 1} in my case.

What am I doning wrong? I am using pymongo 3.4 and python 3.6 and mongomock 3.7.

I have also tried replacing 1 with "1" which does not crash, but gives an incorrect result: ({"_id" : NumberInt("1234"), "count" : NumberInt("0")})

user1747134
  • 2,374
  • 1
  • 19
  • 26

1 Answers1

0

Well, apparently it's a bug in mongomock and after upgrade to 3.8 I get the expected behavior.

user1747134
  • 2,374
  • 1
  • 19
  • 26