Documents:
{ "group" : "G1", "cat" : "Cat1", "desc": "Some description 1"}
{ "group" : "G1", "cat" : "Cat2", "desc": "Some description 2"}
{ "group" : "G1", "cat" : "Cat1", "desc": "Some description 3"}
{ "group" : "G1", "cat" : "Cat3", "desc": "Some description 4"}
{ "group" : "G1", "cat" : "Cat2", "desc": "Some description 4"}
Can someone help me, using Mongoose, how to find the records that have unique group and cat?
From the Mongoose API for distinct, I understand the that I can use just one field. But can the Model.distinct be used to find documents based on two fields?
I can’t give you a Mongoose specific example, and your question is a bit vague. The aggregation equivalent of “But can the Model.distinct be used to find documents based on two fields?” is:
Which returns:
If you want to find group/cat combinations that only occur once, then you would use:
Which returns:
From http://mongoosejs.com/docs/api.html#model_Model.aggregate I learn that you can use the aggregation framework in Mongoose like: