diff --git a/source/includes/note-distinct-bson-limit-agg-alternative.rst b/source/includes/note-distinct-bson-limit-agg-alternative.rst new file mode 100644 index 00000000000..e6a2f9b0e60 --- /dev/null +++ b/source/includes/note-distinct-bson-limit-agg-alternative.rst @@ -0,0 +1,8 @@ +.. note:: + + Results must not be larger than the maximum :ref:`BSON size + `. If your results exceed the maximum + BSON size, use the aggregation pipeline to retrieve distinct + values using the :pipeline:`$group` operator, as described in + :ref:`Retrieve Distinct Values with the Aggregation Pipeline + `. \ No newline at end of file diff --git a/source/includes/table-sql-to-mongo-select-examples.yaml b/source/includes/table-sql-to-mongo-select-examples.yaml index 1370c65ed3a..2fd37373063 100644 --- a/source/includes/table-sql-to-mongo-select-examples.yaml +++ b/source/includes/table-sql-to-mongo-select-examples.yaml @@ -343,6 +343,13 @@ sql19: | SELECT DISTINCT(status) FROM people mongo19: | + .. code-block:: javascript + :emphasize-lines: 1 + + db.people.aggregate( [ { $group : { _id : "$status" } } ] ) + + or, for distinct value sets that do not exceed the :ref:`BSON size limit ` + .. code-block:: javascript :emphasize-lines: 1 diff --git a/source/reference/command/distinct.txt b/source/reference/command/distinct.txt index 2e7b559ed97..e856476f180 100644 --- a/source/reference/command/distinct.txt +++ b/source/reference/command/distinct.txt @@ -36,9 +36,11 @@ Definition .. include:: /includes/apiargs/dbcommand-distinct-field.rst + .. include:: /includes/note-distinct-bson-limit-agg-alternative.rst + MongoDB also provides the shell wrapper method :method:`db.collection.distinct()` for the :dbcommand:`distinct` - command. Additionally, many MongoDB :term:`drivers ` also + command. Additionally, many MongoDB :term:`drivers ` provide a wrapper method. Refer to the specific driver documentation. Behavior diff --git a/source/reference/method/db.collection.distinct.txt b/source/reference/method/db.collection.distinct.txt index 203a64f3fec..0aa3a82cafb 100644 --- a/source/reference/method/db.collection.distinct.txt +++ b/source/reference/method/db.collection.distinct.txt @@ -22,8 +22,9 @@ Definition .. include:: /includes/apiargs/method-db.collection.distinct-param.rst The :method:`db.collection.distinct()` method provides a wrapper - around the :dbcommand:`distinct` command. Results must not be larger - than the maximum :ref:`BSON size `. + around the :dbcommand:`distinct` command. + + .. include:: /includes/note-distinct-bson-limit-agg-alternative.rst .. _distinct-method-options: