Skip to content

Commit 1741fc6

Browse files
DOCSP-13865 distinct usage examples (#25)
* added distinct usage examples page
1 parent 457b4fa commit 1741fc6

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

source/usage-examples.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ Usage Examples
1717
/usage-examples/delete-operations
1818
/usage-examples/count
1919
/usage-examples/command
20+
/usage-examples/distinct
2021

2122
..
2223
/usage-examples/insert-operations
2324
/usage-examples/bulkWrite
2425
/usage-examples/watch
25-
/usage-examples/distinct
2626

2727

2828
Overview

source/usage-examples/distinct.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,45 @@ Retrieve Distinct Values of a Field
33
===================================
44

55
.. 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>`__

0 commit comments

Comments
 (0)