I have a db with 80,000 documents in it and each document has a field named Location like below:
Location: "82 Some Street | Gautham PS M1M 1M1"
This address field is repeated in many documents. I want to do a query that does a count of the documents with unique addresses only in Location field. How to do that?
So even though I have 80,000 documents, probably only 50,000 of them are unique and other 30,000 have the repeat address so I want to get that 50,000 count accurately.
I guess first, I have to search all the address and then take out the repeats and then do a count?
I tried distinct command but failed.
3
Answers
You can do it with Aggregation framework:
$group
with$addToSet
– to get all the unique locations and add them to theunique_locations
array.$project
with$size
– to get the total count of unique locations by calculating the length of theunique_locations
array.Working example
countDocuments() returns the count.
You can use this query:
In this query, first, we group the documents by
address
and calculate thecounts
. Next, we only keep the counts where the value is 1. Then using the$count
operator we calculate the count.Playground link.
Try this as well:
Playground link.