File tree Expand file tree Collapse file tree 3 files changed +90
-1
lines changed
includes/usage-examples/code-snippets Expand file tree Collapse file tree 3 files changed +90
-1
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "context"
5
+ "fmt"
6
+ "log"
7
+ "os"
8
+
9
+ "go.mongodb.org/mongo-driver/bson"
10
+ "go.mongodb.org/mongo-driver/mongo"
11
+ "go.mongodb.org/mongo-driver/mongo/options"
12
+ )
13
+
14
+ func main () {
15
+ var uri string
16
+ if uri = os .Getenv ("MONGODB_URI" ); uri == "" {
17
+ log .Fatal ("You must set your 'MONGODB_URI' environmental variable. See\n \t https://docs.mongodb.com/drivers/go/current/usage-examples/" )
18
+ }
19
+
20
+ client , err := mongo .Connect (context .TODO (), options .Client ().ApplyURI (uri ))
21
+ if err != nil {
22
+ panic (err )
23
+ }
24
+ defer func () {
25
+ if err = client .Disconnect (context .TODO ()); err != nil {
26
+ panic (err )
27
+ }
28
+ }()
29
+
30
+ // begin distinct
31
+ coll := client .Database ("sample_mflix" ).Collection ("movies" )
32
+ filter := bson.D {{"directors" , "Natalie Portman" }}
33
+
34
+ results , err := coll .Distinct (context .TODO (), "title" , filter )
35
+ // end distinct
36
+
37
+ if err != nil {
38
+ panic (err )
39
+ }
40
+
41
+ // When you run this file, it should print:
42
+ // A Tale of Love and Darkness
43
+ // New York, I Love You
44
+ for _ , result := range results {
45
+ fmt .Println (result )
46
+ }
47
+ }
Original file line number Diff line number Diff line change @@ -17,12 +17,12 @@ Usage Examples
17
17
/usage-examples/delete-operations
18
18
/usage-examples/count
19
19
/usage-examples/command
20
+ /usage-examples/distinct
20
21
21
22
..
22
23
/usage-examples/insert-operations
23
24
/usage-examples/bulkWrite
24
25
/usage-examples/watch
25
- /usage-examples/distinct
26
26
27
27
28
28
Overview
Original file line number Diff line number Diff line change @@ -3,3 +3,45 @@ Retrieve Distinct Values of a Field
3
3
===================================
4
4
5
5
.. default-domain:: mongodb
6
+
7
+ You can retrieve a list of distinct values for a field across a
8
+ collection by using the ``Distinct()`` method.
9
+
10
+ The following example specifies a query filter and a field name to the
11
+ ``Distinct()`` method, which matches documents in the ``movies``
12
+ collection where the ``directors`` field contains "Natalie Portman" and
13
+ returns distinct values of the ``title`` field from the matched documents.
14
+
15
+ .. include:: /includes/usage-examples/run-example-tip.rst
16
+
17
+ .. literalinclude:: /includes/usage-examples/code-snippets/distinct.go
18
+ :start-after: begin distinct
19
+ :end-before: end distinct
20
+ :emphasize-lines: 4
21
+ :language: go
22
+ :dedent:
23
+
24
+ Click `here <{+example+}/distinct.go>`__ to see a fully runnable example.
25
+
26
+ Expected Result
27
+ ---------------
28
+
29
+ The example returns an empty slice of an ``interface`` type that
30
+ contains the following values:
31
+
32
+ .. code-block:: none
33
+ :copyable: false
34
+
35
+ A Tale of Love and Darkness
36
+ New York, I Love You
37
+
38
+ Additional Information
39
+ ----------------------
40
+
41
+ For more information on retrieving distinct values, see our guide on
42
+ <TODO: Distinct Fundamentals>.
43
+
44
+ API Documentation
45
+ ~~~~~~~~~~~~~~~~~
46
+
47
+ `Distinct() <{+godocs+}/mongo#Collection.Distinct>`__
You can’t perform that action at this time.
0 commit comments