Counting unique elements in a subdocument array [duplicate] Counting unique elements in a subdocument array [duplicate] mongodb mongodb

Counting unique elements in a subdocument array [duplicate]


Another way of doing this is to use the distinct function to get an array of the distinct values of the embedded name field, and then check its length.

In the shell:

db.jobs.distinct('job.people.name').length

That's assuming the job subdocument level that you show in your code but not your example document is, in fact, there. Otherwise it would just be 'people.name'.