Aggregation task - $distinct

A field producing aggregation task that retrieves distinct values for specified fields.

The $distinct task in the aggregation query is a convenience function and semantically equivalent to a $group exclusively on the specified fields.

Syntax diagram

Read syntax diagramSkip visual syntax diagram$distinct:{key}

Command arguments

key
One or more JSON fields, only inclusion is supported

Example

Example 1

db.books.aggregate({"$distinct": {"author": 1}})

Sample output:

nosql>Row 1:
nosql> {
"author":"Bujold"
}
nosql>Row 2:
nosql> {
"author":"Christie"
}

Example 2

db.books.aggregate({"$distinct": {"myauthor": "author", "topic": 1}} )

Sample output:

nosql>Row 1:
nosql> {
"myauthor":"Miller",
"topic":"Fantasy"
}
nosql>Row 2:
nosql> {
"myauthor":"Smith",
"topic":"Fantasy"
}
nosql>Row 3:
nosql> {
"myauthor":"Smith",
"topic":"Mystery"
}