Finding a distinct set of fields in MongoDB
You can do this using the following Aggregation Pipeline:
var distinctIdCode = { $group: { _id: { team_code: "$team_code", team_id: "$team_id" } } }
db.foo.aggregate([distinctIdCode])
This will give you:
{ "_id" : { "team_code" : 4, "team_id" : 5 } }
{ "_id" : { "team_code" : 3, "team_id" : 2 } }
This query returns new documents created from the documents in your collection. The documents returned have an _id
which is the result of grouping the collection documents on the team_code
and team_id
fields.