What is the best way to just COUNT unique values within many model\'s array? Let\'s say my Model is like this:
{
myKey: [
\"idnumber1000\",
\"idnumber1
You can do this with a simple aggregate
pipeline:
MyModel.aggregate([
// Project just the myKey field as that's all that's needed
{$project: {_id: 0, myKey: 1}},
// Duplicate each doc, once per myKey element
{$unwind: '$myKey'},
// Group on myKey and get a count
{$group: {_id: '$myKey', count: {$sum: 1}}}
],
function(err, results) {...}
);