0

How do I get the number of all unique name fields in table collections? In SQL the code would look something like this:

select count(*) from (
    select name from table
    group by name
) 

My first guess was this

aggregated_sums = table.aggregate([{"$group": {"_id": "$name", "count":{"$sum": 1}}}])
print(len(list(aggregated_sums)))

But I'm afraid that in my scenario aggregated_sums might contain tens of millions of records.

diplodocus
  • 67
  • 1
  • 1
  • 7

1 Answers1

0

This is almost a correct approach as per MongoDB documentation. Distinct has limitations, and might not be applicable in your case - see e.g. discussion under this answer.

The trick here is to use count on a separate step from group:

table.aggregate([
  {
    "$group": {
      "_id": "$name"
    }
  },
  {
    "$count": "unique_keys"
  }
])

I have tested it on mongoplayground, count clause on the group stage is not mandatory. Of course, the result returned is an aggregated count already, no need to call len afterwards.

Lodinn
  • 462
  • 2
  • 9