Skip to content

Commit 121f955

Browse files
authored
DOCSP-26414 Update "Replace a Document" Usage Example with Document Structs (#201)
* DOCSP-26414 add document struct and adjust replace page * reduce number of fields * reduce number of fields on insertMany example * shorten insertOne example * remove 'results truncated' comment
1 parent 69da7b9 commit 121f955

File tree

6 files changed

+54
-46
lines changed

6 files changed

+54
-46
lines changed

source/includes/usage-examples/code-snippets/insertMany.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
// start-restaurant-struct
1515
type Restaurant struct {
1616
Name string
17-
RestaurantId string `bson:"restaurant_id,omitempty"`
18-
Cuisine string
19-
Address interface{} `bson:"address,omitempty"`
20-
Borough string
17+
RestaurantId string `bson:"restaurant_id,omitempty"`
18+
Cuisine string `bson:"cuisine,omitempty"`
19+
Address interface{} `bson:"address,omitempty"`
20+
Borough string `bson:"borough,omitempty"`
2121
Grades []interface{} `bson:"grades,omitempty"`
2222
}
2323

@@ -46,9 +46,8 @@ func main() {
4646
// begin insertMany
4747
coll := client.Database("sample_restaurants").Collection("restaurants")
4848
newRestaurants := []interface{}{
49-
Restaurant{Name: "Rule of Thirds", Cuisine: "Japanese", Borough: "Brooklyn"},
50-
Restaurant{Name: "Soothr", Cuisine: "Thai", Borough: "Manhattan"},
51-
Restaurant{Name: "Madame Vo", Cuisine: "Vietnamese", Borough: "Manhattan"},
49+
Restaurant{Name: "Rule of Thirds", Cuisine: "Japanese"},
50+
Restaurant{Name: "Madame Vo", Cuisine: "Vietnamese"},
5251
}
5352

5453
result, err := coll.InsertMany(context.TODO(), newRestaurants)
@@ -58,7 +57,7 @@ func main() {
5857
// end insertMany
5958

6059
// When you run this file, it should print:
61-
// 3 documents inserted with IDs: ObjectID("..."), ObjectID("...")
60+
// 2 documents inserted with IDs: ObjectID("..."), ObjectID("...")
6261
fmt.Printf("%d documents inserted with IDs:\n", len(result.InsertedIDs))
6362
for _, id := range result.InsertedIDs {
6463
fmt.Printf("\t%s\n", id)

source/includes/usage-examples/code-snippets/insertOne.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
// start-restaurant-struct
1515
type Restaurant struct {
1616
Name string
17-
RestaurantId string `bson:"restaurant_id,omitempty"`
18-
Cuisine string
19-
Address interface{} `bson:"address,omitempty"`
20-
Borough string
17+
RestaurantId string `bson:"restaurant_id,omitempty"`
18+
Cuisine string `bson:"cuisine,omitempty"`
19+
Address interface{} `bson:"address,omitempty"`
20+
Borough string `bson:"borough,omitempty"`
2121
Grades []interface{} `bson:"grades,omitempty"`
2222
}
2323

@@ -45,7 +45,7 @@ func main() {
4545

4646
// begin insertOne
4747
coll := client.Database("sample_restaurants").Collection("restaurants")
48-
newRestaurant := Restaurant{Name: "8282", Cuisine: "Korean", Borough: "Manhattan"}
48+
newRestaurant := Restaurant{Name: "8282", Cuisine: "Korean"}
4949

5050
result, err := coll.InsertOne(context.TODO(), newRestaurant)
5151
if err != nil {

source/includes/usage-examples/code-snippets/replace.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import (
1212
"go.mongodb.org/mongo-driver/mongo/options"
1313
)
1414

15+
// start-restaurant-struct
16+
type Restaurant struct {
17+
Name string
18+
RestaurantId string `bson:"restaurant_id,omitempty"`
19+
Cuisine string `bson:"cuisine,omitempty"`
20+
Address interface{} `bson:"address,omitempty"`
21+
Borough string `bson:"borough,omitempty"`
22+
Grades []interface{} `bson:"grades,omitempty"`
23+
}
24+
25+
// end-restaurant-struct
26+
1527
func main() {
1628
if err := godotenv.Load(); err != nil {
1729
log.Println("No .env file found")
@@ -33,9 +45,9 @@ func main() {
3345
}()
3446

3547
// begin replace
36-
coll := client.Database("insertDB").Collection("haikus")
37-
filter := bson.D{{"title", "Record of a Shriveled Datum"}}
38-
replacement := bson.D{{"title", "Dodging Greys"}, {"text", "When there're no matches, no longer need to panic. You can use upsert"}}
48+
coll := client.Database("sample_restaurants").Collection("restaurants")
49+
filter := bson.D{{"name", "Madame Vo"}}
50+
replacement := Restaurant{Name: "Monsieur Vo", Cuisine: "Asian Fusion"}
3951

4052
result, err := coll.ReplaceOne(context.TODO(), filter, replacement)
4153
if err != nil {

source/usage-examples/insertMany.txt

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ in the ``restaurants`` collection:
2727
The ``omitempty`` :ref:`struct tag<golang-struct-tags>` omits the corresponding
2828
field from the inserted document when left empty.
2929

30-
The following example inserts three new documents to the ``restaurants``
30+
The following example inserts two new documents to the ``restaurants``
3131
collection:
3232

3333
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
3434

3535
.. literalinclude:: /includes/usage-examples/code-snippets/insertMany.go
3636
:start-after: begin insertMany
3737
:end-before: end insertMany
38-
:emphasize-lines: 8
38+
:emphasize-lines: 7
3939
:language: go
4040
:dedent:
4141

@@ -48,26 +48,10 @@ After you run the full example, you can find the following inserted
4848
documents in the ``restaurants`` collection:
4949

5050
.. code-block:: json
51-
:copyable: false
52-
53-
{
54-
"_id": ObjectId("..."),
55-
"name": "Rule of Thirds",
56-
"cuisine": "Japanese",
57-
"borough": "Brooklyn"
58-
},
59-
{
60-
"_id": ObjectId("..."),
61-
"title": "Soothr",
62-
"cuisine": "Thai",
63-
"borough": "Manhattan"
64-
},
65-
{
66-
"_id": ObjectId("..."),
67-
"title": "Madame Vo",
68-
"cuisine": "Vietnamese",
69-
"borough": "Manhattan"
70-
}
51+
:copyable: false
52+
53+
{ "_id": ObjectID("..."), "name": "Rule of Thirds", "cuisine": "Japanese"},
54+
{ "_id": ObjectID("..."), "name": "Madame Vo", "cuisine": "Vietnamese"}
7155

7256
For an example on how to find multiple documents, see the
7357
:ref:`golang-find-multiple` usage example.

source/usage-examples/insertOne.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ document in the ``restaurants`` collection:
5454
{
5555
"_id": ObjectId("..."),
5656
"name": "8282",
57-
"cuisine": "Korean",
58-
"borough": "Manhattan"
57+
"cuisine": "Korean"
5958
}
6059

6160
For an example on how to find a document, see the :ref:`golang-find-one` usage

source/usage-examples/replaceOne.txt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,23 @@ Example
1414

1515
.. include:: /includes/usage-examples/run-example-tip.rst
1616

17-
The following example performs the following on the ``haikus``
17+
This example uses the following ``Restaurant`` struct as a model for documents
18+
in the ``restaurants`` collection:
19+
20+
.. literalinclude:: /includes/usage-examples/code-snippets/replace.go
21+
:start-after: start-restaurant-struct
22+
:end-before: end-restaurant-struct
23+
:language: go
24+
:copyable:
25+
:dedent:
26+
27+
The ``omitempty`` :ref:`struct tag<golang-struct-tags>` omits the corresponding
28+
field from the inserted document when left empty.
29+
30+
This example performs the following actions on the ``restaurants``
1831
collection:
1932

20-
- Matches a document in which the ``title`` is "Record of a Shriveled Datum"
33+
- Matches a document in which the ``name`` is "Madame Vo"
2134
- Replaces the matched document with a new document
2235

2336
.. literalinclude:: /includes/usage-examples/code-snippets/replace.go
@@ -33,18 +46,19 @@ Expected Result
3346
---------------
3447

3548
After you run the full example, you can find the following replaced
36-
document in the ``haikus`` collection:
49+
document in the ``restaurants`` collection:
3750

3851
.. code-block:: json
3952
:copyable: false
4053

4154
{
4255
"_id" : ObjectId("..."),
43-
"title" : "Dodging Greys",
44-
"text" : "When there're no matches, no longer need to panic. You can use upsert."
56+
"name" : "Monsieur Vo",
57+
"cuisine" : "Asian Fusion"
4558
}
4659

47-
For an example on how to find a document, see :ref:`golang-find-one`.
60+
For an example on how to find a document, see the :ref:`golang-find-one` usage
61+
example.
4862

4963
Additional Information
5064
----------------------

0 commit comments

Comments
 (0)