0

I would like to count the value of a certain field only once, below is an example of how it would be:

 {name: 'a', otherStuff: 'blah'},
 {name: 'a', otherStuff: 'blah'},
 {name: 'b', otherStuff: 'blah'},
 {name: 'c', otherStuff: 'blah'},
 {name: 'c', otherStuff: 'blah'},
 {name: 'a', otherStuff: 'blah'}

the result I need is:

 {names_count: 3}

I tried to do some sort of grouping but it didn't work

Santos
  • 1
  • 1
  • What do you mean by *count the value of a certain field only once*? Currently it is unclear and needs more explanation. – ray Apr 06 '23 at 14:40
  • It was as I put it in the example, I want to count a field, for example the name field, but I don't want to count repeated values, if there are 3 fields with the value "a", I want to count only 1 occurrence – Santos Apr 06 '23 at 14:42
  • so you are looking for distinct count? There are 3 unique values: `a, b, c` so the expected output is 3? – ray Apr 06 '23 at 14:43
  • exactly that, but I don't know how to do it in mongodb – Santos Apr 06 '23 at 15:00
  • Does this answer your question? [mongodb count num of distinct values per field/key](https://stackoverflow.com/questions/14924495/mongodb-count-num-of-distinct-values-per-field-key) – ray Apr 06 '23 at 15:17

0 Answers0