|
| 1 | +.. _java-rs-distinct: |
| 2 | + |
| 3 | +============================== |
| 4 | +Retrieve Distinct Field Values |
| 5 | +============================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: read, unique, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +Within a collection, different documents might contain different values for a single field. |
| 24 | +For example, one document in the ``restaurant`` collection has a ``borough`` value of ``"Manhattan"``, and |
| 25 | +another has a ``borough`` value of ``"Queens"``. With the {+driver-short+}, you can |
| 26 | +retrieve all the distinct values that a field contains across multiple documents |
| 27 | +in a collection. |
| 28 | + |
| 29 | +Sample Data |
| 30 | +~~~~~~~~~~~ |
| 31 | + |
| 32 | +The examples in this guide use the ``sample_restaurants.restaurants`` collection |
| 33 | +from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a |
| 34 | +free MongoDB Atlas cluster and load the sample datasets, see |
| 35 | +:ref:`<java-rs-getting-started>`. |
| 36 | + |
| 37 | +.. include:: includes/reactor-note.rst |
| 38 | + |
| 39 | +``distinct()`` Method |
| 40 | +--------------------- |
| 41 | + |
| 42 | +To retrieve the distinct values for a specified field, call the ``distinct()`` |
| 43 | +method and pass in the name of the field you want to find distinct values for. |
| 44 | + |
| 45 | +Retrieve Distinct Values Across a Collection |
| 46 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 47 | + |
| 48 | +The following example retrieves the distinct values of the ``borough`` field in |
| 49 | +the ``restaurants`` collection: |
| 50 | + |
| 51 | +.. io-code-block:: |
| 52 | + |
| 53 | + .. input:: /includes/read-ops/distinct.java |
| 54 | + :start-after: start-distinct |
| 55 | + :end-before: end-distinct |
| 56 | + :language: java |
| 57 | + :dedent: |
| 58 | + |
| 59 | + .. output:: |
| 60 | + |
| 61 | + Bronx |
| 62 | + Brooklyn |
| 63 | + Manhattan |
| 64 | + Missing |
| 65 | + Queens |
| 66 | + Staten Island |
| 67 | + |
| 68 | +The results show every distinct value that appears in the ``borough`` field |
| 69 | +across all documents in the collection. Although several documents have the same |
| 70 | +value in the ``borough`` field, each value appears in the results only once. |
| 71 | + |
| 72 | +Retrieve Distinct Values Across Specified Documents |
| 73 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 74 | + |
| 75 | +You can provide a **query filter** to the ``distinct()`` method to find the distinct |
| 76 | +field values across a subset of documents in a collection. A query filter is an expression that specifies search |
| 77 | +criteria used to match documents in an operation. For more information about |
| 78 | +creating a query filter, see :ref:`java-rs-specify-query`. |
| 79 | + |
| 80 | +The following example retrieves the distinct values of the ``borough`` field for |
| 81 | +all documents that have a ``cuisine`` field value of ``"Italian"``: |
| 82 | + |
| 83 | +.. io-code-block:: |
| 84 | + |
| 85 | + .. input:: /includes/read-ops/distinct.java |
| 86 | + :start-after: start-distinct-query |
| 87 | + :end-before: end-distinct-query |
| 88 | + :language: java |
| 89 | + :dedent: |
| 90 | + |
| 91 | + .. output:: |
| 92 | + |
| 93 | + Bronx |
| 94 | + Brooklyn |
| 95 | + Manhattan |
| 96 | + Queens |
| 97 | + Staten Island |
| 98 | + |
| 99 | +Modify Distinct Behavior |
| 100 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 101 | + |
| 102 | +The ``distinct()`` method can be modified by chaining methods to the ``distinct()`` method |
| 103 | +call. If you don't specify any options, the driver does not customize the operation. |
| 104 | + |
| 105 | +The following table describes some methods you can use to customize the |
| 106 | +``distinct()`` operation: |
| 107 | + |
| 108 | +.. list-table:: |
| 109 | + :widths: 30 70 |
| 110 | + :header-rows: 1 |
| 111 | + |
| 112 | + * - Method |
| 113 | + - Description |
| 114 | + |
| 115 | + * - ``batchSize()`` |
| 116 | + - | Sets the number of documents to return per batch. |
| 117 | + |
| 118 | + * - ``collation()`` |
| 119 | + - | Specifies the kind of language collation to use when sorting |
| 120 | + results. For more information, see :manual:`Collation </reference/collation/#std-label-collation>` |
| 121 | + in the {+mdb-server+} manual. |
| 122 | + |
| 123 | + * - ``comment()`` |
| 124 | + - | Specifies a comment to attach to the operation. |
| 125 | + |
| 126 | + * - ``filter()`` |
| 127 | + - | Sets the query filter to apply to the query. |
| 128 | + |
| 129 | + * - ``maxTime()`` |
| 130 | + - | Sets the maximum amount of time to allow the operation to run, in milliseconds. |
| 131 | + |
| 132 | +For a complete list of methods you can use to modify the ``distinct()`` method, see |
| 133 | +the `DistinctPublisher <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/DistinctPublisher.html>`__ API documentation. |
| 134 | + |
| 135 | +The following example retrieves the distinct values of the ``name`` field for |
| 136 | +all documents that have a ``borough`` field value of ``"Bronx"`` and a |
| 137 | +``cuisine`` field value of ``"Pizza"``. It also uses |
| 138 | +the ``comment`` option to add a comment to the operation. |
| 139 | + |
| 140 | +.. io-code-block:: |
| 141 | + |
| 142 | + .. input:: /includes/read-ops/distinct.java |
| 143 | + :start-after: start-distinct-comment |
| 144 | + :end-before: end-distinct-comment |
| 145 | + :language: java |
| 146 | + :dedent: |
| 147 | + |
| 148 | + .. output:: |
| 149 | + |
| 150 | + $1.25 Pizza |
| 151 | + 18 East Gunhill Pizza |
| 152 | + 2 Bros |
| 153 | + Aenos Pizza |
| 154 | + Alitalia Pizza Restaurant |
| 155 | + ... |
| 156 | + |
| 157 | +Additional Information |
| 158 | +---------------------- |
| 159 | + |
| 160 | +To learn more about the distinct command, see the :manual:`Distinct guide |
| 161 | +</reference/command/distinct/>` in the MongoDB Server Manual. |
| 162 | + |
| 163 | +API Documentation |
| 164 | +~~~~~~~~~~~~~~~~~ |
| 165 | + |
| 166 | +To learn more about any of the methods or types discussed in this |
| 167 | +guide, see the following API documentation: |
| 168 | + |
| 169 | +- `distinct() <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoCollection.html#distinct(java.lang.String,java.lang.Class)>`__ |
| 170 | +- `DistinctPublisher <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/DistinctPublisher.html>`__ |
0 commit comments