Skip to content

Commit e0207c4

Browse files
authored
DOCSP-26437: use struct in cursor pg (#214)
1 parent 90a1a69 commit e0207c4

File tree

2 files changed

+36
-27
lines changed
  • source

2 files changed

+36
-27
lines changed

source/fundamentals/crud/read-operations/cursor.txt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ Each section uses the following ``cursor`` variable, which is a
3131
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/cursor.go
3232
:language: go
3333
:dedent:
34-
:start-after: begin find
35-
:end-before: end find
34+
:start-after: begin cursor def
35+
:end-before: end cursor def
36+
37+
In the examples in this guide, the driver unmarshals documents held in
38+
the ``cursor`` variable to a sample ``MyStruct`` struct.
3639

3740
.. important::
3841

39-
A cursor is not goroutine safe. Do not use the same cursor in
42+
A cursor is not `goroutine <https://www.golang-book.com/books/intro/10>`__ safe. Do not use the same cursor in
4043
multiple goroutines at the same time.
4144

4245
.. _golang-individual-documents:
@@ -47,11 +50,11 @@ Retrieve Documents Individually
4750
To retrieve documents from your cursor individually while blocking the
4851
current goroutine, use the ``Next()`` method.
4952

50-
The method returns a document if each of the following is true:
53+
The method returns a document if all of the following conditions are met:
5154

52-
- A document is currently or will later be available
53-
- No errors occurred
54-
- The context didn't expire
55+
- A document is currently or will later be available.
56+
- The driver didn't throw any errors.
57+
- The context didn't expire.
5558

5659
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/cursor.go
5760
:language: go
@@ -65,11 +68,11 @@ Tailable Cursor
6568
To attempt retrieving a document from a :manual:`tailable cursor
6669
</core/tailable-cursors/>`, use the ``TryNext()`` method.
6770

68-
The method returns a document if each of the following is true:
71+
The method returns a document if all of the following conditions are met:
6972

70-
- A document is currently available
71-
- No errors occurred
72-
- The context didn't expire
73+
- A document is currently available.
74+
- The driver didn't throw any errors.
75+
- The context didn't expire.
7376

7477
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/cursor.go
7578
:language: go

source/includes/fundamentals/code-snippets/CRUD/cursor.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import (
1111
"go.mongodb.org/mongo-driver/mongo/options"
1212
)
1313

14+
// start-sample-struct
15+
type MyStruct struct {
16+
MyProperty string
17+
}
18+
19+
// end-sample-struct
20+
1421
func main() {
1522
var uri string
1623
if uri = os.Getenv("MONGODB_URI"); uri == "" {
@@ -28,19 +35,18 @@ func main() {
2835
}
2936
}()
3037

31-
client.Database("tea").Collection("ratings").Drop(context.TODO())
32-
33-
coll := client.Database("tea").Collection("ratings")
38+
coll := client.Database("db").Collection("sample_data")
3439
docs := []interface{}{
35-
bson.D{{"type", "Masala"}, {"rating", 10}},
36-
bson.D{{"type", "Earl Grey"}, {"rating", 5}},
37-
bson.D{{"type", "Assam"}, {"rating", 7}},
40+
MyStruct{MyProperty: "abc"},
41+
MyStruct{MyProperty: "def"},
42+
MyStruct{MyProperty: "ghi"},
3843
}
3944

4045
result, err := coll.InsertMany(context.TODO(), docs)
4146
if err != nil {
4247
panic(err)
4348
}
49+
4450
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
4551

4652
fmt.Println("Cursor Elements:")
@@ -54,7 +60,7 @@ func main() {
5460
// begin close
5561
defer cursor.Close(context.TODO())
5662
// end close
57-
63+
5864
for cursor.Next(context.TODO()) {
5965
// begin current
6066
fmt.Println(cursor.Current)
@@ -73,22 +79,22 @@ func main() {
7379

7480
fmt.Println("Cursor.All():")
7581
{
76-
// begin find
82+
// begin cursor def
7783
cursor, err := coll.Find(context.TODO(), bson.D{})
7884
if err != nil {
7985
panic(err)
8086
}
81-
// end find
87+
// end cursor def
8288

8389
defer cursor.Close(context.TODO())
84-
90+
8591
// begin cursor all
86-
var results []bson.D
92+
var results []MyStruct
8793
if err = cursor.All(context.TODO(), &results); err != nil {
8894
panic(err)
8995
}
9096
for _, result := range results {
91-
fmt.Println(result)
97+
fmt.Printf("%+v\n", result)
9298
}
9399
// end cursor all
94100
}
@@ -104,11 +110,11 @@ func main() {
104110

105111
// begin cursor next
106112
for cursor.Next(context.TODO()) {
107-
var result bson.D
113+
var result MyStruct
108114
if err := cursor.Decode(&result); err != nil {
109115
log.Fatal(err)
110116
}
111-
fmt.Println(result)
117+
fmt.Printf("%+v\n", result)
112118
}
113119
if err := cursor.Err(); err != nil {
114120
log.Fatal(err)
@@ -128,11 +134,11 @@ func main() {
128134
// begin cursor try next
129135
for {
130136
if cursor.TryNext(context.TODO()) {
131-
var result bson.D
137+
var result MyStruct
132138
if err := cursor.Decode(&result); err != nil {
133139
log.Fatal(err)
134140
}
135-
fmt.Println(result)
141+
fmt.Printf("%+v\n", result)
136142
continue
137143
}
138144

0 commit comments

Comments
 (0)