Skip to content

Commit 7ec8f22

Browse files
committed
move usag examples and add bsond
1 parent 61b2f4c commit 7ec8f22

File tree

5 files changed

+176
-12
lines changed

5 files changed

+176
-12
lines changed

source/crud/insert.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,36 @@ Construct an ``InsertOneOptions`` as follows:
133133
:copyable:
134134
:dedent:
135135

136+
Insert a Document Example: Full File
137+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138+
139+
.. include:: /includes/usage-examples/example-intro.rst
140+
141+
The following example inserts a new document into the ``restaurants`` collection.
142+
Select the :guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code:
143+
144+
.. tabs::
145+
146+
.. tab:: Struct
147+
:tabid: structExample
148+
149+
The following code uses a struct to insert a new document into the
150+
``restaurants`` collection:
151+
152+
.. literalinclude:: /includes/usage-examples/code-snippets/insertOne.go
153+
:language: go
154+
:dedent:
155+
156+
.. tab:: bson.D
157+
:tabid: bsonDExample
158+
159+
The following code uses a ``bson.D`` type to insert a new document into the
160+
``restaurants`` collection:
161+
162+
.. literalinclude:: /includes/usage-examples/code-snippets/insertOneBsonD.go
163+
:language: go
164+
:dedent:
165+
136166
Insert Multiple Documents
137167
-------------------------
138168

@@ -280,6 +310,31 @@ inserted into your collection.
280310
{ "_id": 1, "title": "Where the Wild Things Are" }
281311
{ "_id": 2, "title": "The Very Hungry Caterpillar" }
282312

313+
Insert Many Example: Full File
314+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
315+
316+
.. include:: /includes/usage-examples/example-intro.rst
317+
318+
The following example inserts multiple new documents into the ``restaurants`` collection.
319+
Select the :guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code:
320+
321+
.. tabs::
322+
323+
.. tab:: Struct
324+
:tabid: structExample
325+
326+
The following code uses a struct to insert multiple new documents into the
327+
``restaurants`` collection:
328+
329+
.. literalinclude:: /includes/usage-examples/code-snippets/insertMany.go
330+
331+
.. tab:: bson.D
332+
:tabid: bsonDExample
333+
334+
The following code uses a ``bson.D`` type to insert multiple new documents into the
335+
``restaurants`` collection:
336+
337+
.. literalinclude:: /includes/usage-examples/code-snippets/insertManyBsonD.go
283338

284339
Additional Information
285340
----------------------

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

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

15-
// start-restaurant-struct
15+
// Defines the structure of a restaurant document
1616
type Restaurant struct {
1717
Name string
1818
RestaurantId string `bson:"restaurant_id,omitempty"`
@@ -22,8 +22,6 @@ type Restaurant struct {
2222
Grades []interface{} `bson:"grades,omitempty"`
2323
}
2424

25-
// end-restaurant-struct
26-
2725
func main() {
2826
if err := godotenv.Load(); err != nil {
2927
log.Println("No .env file found")
@@ -44,7 +42,6 @@ func main() {
4442
}
4543
}()
4644

47-
// begin insertMany
4845
coll := client.Database("sample_restaurants").Collection("restaurants")
4946

5047
// Creates two sample documents describing restaurants
@@ -58,14 +55,13 @@ func main() {
5855
if err != nil {
5956
panic(err)
6057
}
61-
// end insertMany
6258

6359
// Prints the IDs of the inserted documents
6460
fmt.Printf("%d documents inserted with IDs:\n", len(result.InsertedIDs))
6561
for _, id := range result.InsertedIDs {
6662
fmt.Printf("\t%s\n", id)
6763
}
6864

69-
// When you run this file, it should print:
65+
// When you run this file, the expected output is similar to:
7066
// 2 documents inserted with IDs: ObjectID("..."), ObjectID("...")
7167
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Inserts sample documents describing restaurants by using the Go driver with bson.D
2+
package main
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"github.com/joho/godotenv"
11+
"go.mongodb.org/mongo-driver/v2/bson"
12+
"go.mongodb.org/mongo-driver/v2/mongo"
13+
"go.mongodb.org/mongo-driver/v2/mongo/options"
14+
)
15+
16+
func main() {
17+
if err := godotenv.Load(); err != nil {
18+
log.Println("No .env file found")
19+
}
20+
21+
var uri string
22+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
23+
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable")
24+
}
25+
26+
client, err := mongo.Connect(options.Client().ApplyURI(uri))
27+
if err != nil {
28+
panic(err)
29+
}
30+
defer func() {
31+
if err = client.Disconnect(context.TODO()); err != nil {
32+
panic(err)
33+
}
34+
}()
35+
36+
coll := client.Database("sample_restaurants").Collection("restaurants")
37+
38+
// Creates two sample documents describing restaurants using bson.D
39+
newRestaurants := []interface{}{
40+
bson.D{
41+
bson.E{Key: "name", Value: "Rule of Thirds"},
42+
bson.E{Key: "cuisine", Value: "Japanese"},
43+
},
44+
bson.D{
45+
bson.E{Key: "name", Value: "Madame Vo"},
46+
bson.E{Key: "cuisine", Value: "Vietnamese"},
47+
},
48+
}
49+
50+
// Inserts sample documents into the collection
51+
result, err := coll.InsertMany(context.TODO(), newRestaurants)
52+
if err != nil {
53+
panic(err)
54+
}
55+
56+
// Prints the IDs of the inserted documents
57+
fmt.Printf("%d documents inserted with IDs:\n", len(result.InsertedIDs))
58+
for _, id := range result.InsertedIDs {
59+
fmt.Printf("\t%s\n", id)
60+
}
61+
62+
// When you run this file, the expected output is similar to:
63+
// 2 documents inserted with IDs: ObjectID("..."), ObjectID("...")
64+
}

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

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

15-
// start-restaurant-struct
15+
// Defines the structure of a restaurant documen
1616
type Restaurant struct {
1717
Name string
1818
RestaurantId string `bson:"restaurant_id,omitempty"`
@@ -22,8 +22,6 @@ type Restaurant struct {
2222
Grades []interface{} `bson:"grades,omitempty"`
2323
}
2424

25-
// end-restaurant-struct
26-
2725
func main() {
2826
if err := godotenv.Load(); err != nil {
2927
log.Println("No .env file found")
@@ -45,19 +43,17 @@ func main() {
4543
}()
4644

4745
// Inserts a sample document describing a restaurant into the collection
48-
// begin insertOne
4946
coll := client.Database("sample_restaurants").Collection("restaurants")
5047
newRestaurant := Restaurant{Name: "8282", Cuisine: "Korean"}
5148

5249
result, err := coll.InsertOne(context.TODO(), newRestaurant)
5350
if err != nil {
5451
panic(err)
5552
}
56-
// end insertOne
5753

5854
// Prints the ID of the inserted document
5955
fmt.Printf("Document inserted with ID: %s\n", result.InsertedID)
6056

61-
// When you run this file, it should print:
57+
// When you run this file, the expected output is similar to:
6258
// Document inserted with ID: ObjectID("...")
6359
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Inserts a single document describing a restaurant by using the Go driver with bson.D
2+
package main
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"github.com/joho/godotenv"
11+
"go.mongodb.org/mongo-driver/v2/bson"
12+
"go.mongodb.org/mongo-driver/v2/mongo"
13+
"go.mongodb.org/mongo-driver/v2/mongo/options"
14+
)
15+
16+
func main() {
17+
if err := godotenv.Load(); err != nil {
18+
log.Println("No .env file found")
19+
}
20+
21+
var uri string
22+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
23+
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable")
24+
}
25+
26+
client, err := mongo.Connect(options.Client().ApplyURI(uri))
27+
if err != nil {
28+
panic(err)
29+
}
30+
defer func() {
31+
if err = client.Disconnect(context.TODO()); err != nil {
32+
panic(err)
33+
}
34+
}()
35+
36+
// Inserts a sample document describing a restaurant into the collection using bson.D
37+
coll := client.Database("sample_restaurants").Collection("restaurants")
38+
newRestaurant := bson.D{
39+
bson.E{Key: "name", Value: "8282"},
40+
bson.E{Key: "cuisine", Value: "Korean"},
41+
}
42+
43+
result, err := coll.InsertOne(context.TODO(), newRestaurant)
44+
if err != nil {
45+
panic(err)
46+
}
47+
48+
// Prints the ID of the inserted document
49+
fmt.Printf("Document inserted with ID: %s\n", result.InsertedID)
50+
51+
// When you run this file, the expected output is similar to:
52+
// Document inserted with ID: ObjectID("...")
53+
}

0 commit comments

Comments
 (0)