Introduction to MongoDB collection.distinct() Method
The distinct()
method is a method in MongoDB used to obtain the unique values of a specified field in a collection. It can query all the distinct values of a field in a collection and return them as an array. The distinct()
method is useful for statistical analysis and grouping queries of certain fields.
Syntax
The syntax of the distinct()
method is as follows:
db.collection.distinct(field, query, options)
Where,
field
: represents the field to be queried;query
: represents the query condition, which can be empty;options
: represents some optional parameters, such ascollation
,hint
, etc.
Use Cases
The distinct()
method is suitable for the following use cases:
- Obtain all distinct values of a field in a collection;
- Statistical analysis and grouping queries of certain fields.
Examples
Assuming we have a collection named students
, which contains multiple student documents. Each document contains fields such as _id
, name
, age
, gender
, and score
. Now, we need to query all the distinct age values in the students
collection.
db.students.distinct("age")
Executing the above code returns the following result:
[20, 21, 22, 23, 24, 25]
Another example, assuming we have a collection named orders
, which contains multiple order documents. Each document contains fields such as _id
, customer_id
, status
, and items
. Now, we need to query all the distinct status
values in the orders
collection and sort them in order.
db.orders.distinct(
"status",
{},
{ collation: { locale: "en", strength: 2 }, sort: 1 }
)
Executing the above code returns the following result:
["canceled", "completed", "in progress", "pending"]
Conclusion
The distinct()
method is a very useful query method in MongoDB. It can query all the distinct values of a field in a collection and return them as an array. The distinct()
method is useful for statistical analysis and grouping queries of certain fields. When using it, note that the array returned by the distinct()
method only contains the values of the specified field, not the documents themselves. In addition, the distinct()
method may affect query performance when processing large amounts of data, so use it with caution.