Cara menggunakan mongodb count distinct aggregation

For the legacy

collation: {
locale: ,
caseLevel: ,
caseFirst: ,
strength: ,
numericOrdering: ,
alternate: ,
maxVariable: ,
backwards:
}
6 shell documentation, refer to the documentation for the corresponding MongoDB Server release:

  • mongo shell v4.4

  • mongo shell v4.2

Finds the distinct values for a specified field across a single collection or view and returns the results in an array.

This method takes the following parameters:

Parameter

Type

Description

collation: {
locale: ,
caseLevel: ,
caseFirst: ,
strength: ,
numericOrdering: ,
alternate: ,
maxVariable: ,
backwards:
}
7

string

The field for which to return distinct values.

collation: {
locale: ,
caseLevel: ,
caseFirst: ,
strength: ,
numericOrdering: ,
alternate: ,
maxVariable: ,
backwards:
}
8

document

A query that specifies the documents from which to retrieve the distinct values.

collation: {
locale: ,
caseLevel: ,
caseFirst: ,
strength: ,
numericOrdering: ,
alternate: ,
maxVariable: ,
backwards:
}
9

document

Optional. A document that specifies the options. See

Note

Results must not be larger than the maximum . If your results exceed the maximum BSON size, use the aggregation pipeline to retrieve distinct values using the operator, as described in

The following diagram shows an example call.

Options

{ collation: <document> }

Field

Type

Description

{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
2

document

Optional.

Specifies the to use for the operation.

Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.

Aggregation operations process multiple documents and return computed results. You can use aggregation operations to:

  • Group values from multiple documents together.

  • Perform operations on the grouped data to return a single result.

  • Analyze data changes over time.

To perform aggregation operations, you can use:

  • , which are the preferred method for performing aggregations.
  • , which are simple but lack the capabilities of an aggregation pipeline.

Aggregation Pipelines

An aggregation pipeline consists of one or more that process documents:

  • Each stage performs an operation on the input documents. For example, a stage can filter documents, group documents, and calculate values.

  • The documents that are output from a stage are passed to the next stage.

  • An aggregation pipeline can return results for groups of documents. For example, return the total, average, maximum, and minimum values.

Starting in MongoDB 4.2, you can update documents with an aggregation pipeline if you use the stages shown in Updates with Aggregation Pipeline.

Note

Aggregation pipelines run with the method do not modify documents in a collection, unless the pipeline contains a or stage.

Aggregation Pipeline Example

The following aggregation pipeline example contains two and returns the total order quantity of medium size pizzas grouped by pizza name:

db.orders.aggregate( [
// Stage 1: Filter pizza order documents by pizza size
{
$match: { size: "medium" }
},
// Stage 2: Group remaining documents by pizza name and calculate total quantity
{
$group: { _id: "$name", totalQuantity: { $sum: "$quantity" } }
}
] )

The stage:

  • Filters the pizza order documents to pizzas with a size of medium.

  • Passes the remaining documents to the stage.

The stage:

  • Groups the remaining documents by pizza name.

  • Uses to calculate the total order db.collection.aggregate()1 for each pizza name. The total is stored in the db.collection.aggregate()3 field returned by the aggregation pipeline.

For runnable examples containing sample input documents, see

Learn More About Aggregation Pipelines

To learn more about aggregation pipelines, see

Single Purpose Aggregation Methods

The single purpose aggregation methods aggregate documents from a single collection. The methods are simple but lack the capabilities of an aggregation pipeline.