@@ -31,20 +31,6 @@ func requireCursorLength(t *testing.T, cursor *mongo.Cursor, length int) {
31
31
require .Equal (t , i , length )
32
32
}
33
33
34
- func stringSliceEquals (s1 []string , s2 []string ) bool {
35
- if len (s1 ) != len (s2 ) {
36
- return false
37
- }
38
-
39
- for i := range s1 {
40
- if s1 [i ] != s2 [i ] {
41
- return false
42
- }
43
- }
44
-
45
- return true
46
- }
47
-
48
34
func containsKey (doc bsonx.Doc , key ... string ) bool {
49
35
_ , err := doc .LookupErr (key ... )
50
36
if err != nil {
@@ -53,14 +39,12 @@ func containsKey(doc bsonx.Doc, key ...string) bool {
53
39
return true
54
40
}
55
41
42
+ // InsertExamples contains examples for insert operations.
56
43
func InsertExamples (t * testing.T , db * mongo.Database ) {
57
- err := db .RunCommand (
58
- context .Background (),
59
- bson.D {{"dropDatabase" , 1 }},
60
- ).Err ()
61
- require .NoError (t , err )
44
+ coll := db .Collection ("inventory_insert" )
62
45
63
- coll := db .Collection ("inventory" )
46
+ err := coll .Drop (context .Background ())
47
+ require .NoError (t , err )
64
48
65
49
{
66
50
// Start Example 1
@@ -144,14 +128,12 @@ func InsertExamples(t *testing.T, db *mongo.Database) {
144
128
}
145
129
}
146
130
131
+ // QueryToplevelFieldsExamples contains examples for querying top-level fields.
147
132
func QueryToplevelFieldsExamples (t * testing.T , db * mongo.Database ) {
148
- err := db .RunCommand (
149
- context .Background (),
150
- bson.D {{"dropDatabase" , 1 }},
151
- ).Err ()
152
- require .NoError (t , err )
133
+ coll := db .Collection ("inventory_query_top" )
153
134
154
- coll := db .Collection ("inventory" )
135
+ err := coll .Drop (context .Background ())
136
+ require .NoError (t , err )
155
137
156
138
{
157
139
// Start Example 6
@@ -314,14 +296,12 @@ func QueryToplevelFieldsExamples(t *testing.T, db *mongo.Database) {
314
296
315
297
}
316
298
299
+ // QueryEmbeddedDocumentsExamples contains examples for querying embedded document fields.
317
300
func QueryEmbeddedDocumentsExamples (t * testing.T , db * mongo.Database ) {
318
- err := db .RunCommand (
319
- context .Background (),
320
- bson.D {{"dropDatabase" , 1 }},
321
- ).Err ()
322
- require .NoError (t , err )
301
+ coll := db .Collection ("inventory_query_embedded" )
323
302
324
- coll := db .Collection ("inventory" )
303
+ err := coll .Drop (context .Background ())
304
+ require .NoError (t , err )
325
305
326
306
{
327
307
// Start Example 14
@@ -477,14 +457,12 @@ func QueryEmbeddedDocumentsExamples(t *testing.T, db *mongo.Database) {
477
457
478
458
}
479
459
460
+ // QueryArraysExamples contains examples for querying array fields.
480
461
func QueryArraysExamples (t * testing.T , db * mongo.Database ) {
481
- err := db .RunCommand (
482
- context .Background (),
483
- bson.D {{"dropDatabase" , 1 }},
484
- ).Err ()
485
- require .NoError (t , err )
462
+ coll := db .Collection ("inventory_query_array" )
486
463
487
- coll := db .Collection ("inventory" )
464
+ err := coll .Drop (context .Background ())
465
+ require .NoError (t , err )
488
466
489
467
{
490
468
// Start Example 20
@@ -665,14 +643,12 @@ func QueryArraysExamples(t *testing.T, db *mongo.Database) {
665
643
666
644
}
667
645
646
+ // QueryArrayEmbeddedDocumentsExamples contains examples for querying fields with arrays and embedded documents.
668
647
func QueryArrayEmbeddedDocumentsExamples (t * testing.T , db * mongo.Database ) {
669
- err := db .RunCommand (
670
- context .Background (),
671
- bson.D {{"dropDatabase" , 1 }},
672
- ).Err ()
673
- require .NoError (t , err )
648
+ coll := db .Collection ("inventory_query_array_embedded" )
674
649
675
- coll := db .Collection ("inventory" )
650
+ err := coll .Drop (context .Background ())
651
+ require .NoError (t , err )
676
652
677
653
{
678
654
// Start Example 29
@@ -896,14 +872,12 @@ func QueryArrayEmbeddedDocumentsExamples(t *testing.T, db *mongo.Database) {
896
872
}
897
873
}
898
874
875
+ // QueryNullMissingFieldsExamples contains examples for querying fields that are null or missing.
899
876
func QueryNullMissingFieldsExamples (t * testing.T , db * mongo.Database ) {
900
- err := db .RunCommand (
901
- context .Background (),
902
- bson.D {{"dropDatabase" , 1 }},
903
- ).Err ()
904
- require .NoError (t , err )
877
+ coll := db .Collection ("inventory_query_null_missing" )
905
878
906
- coll := db .Collection ("inventory" )
879
+ err := coll .Drop (context .Background ())
880
+ require .NoError (t , err )
907
881
908
882
{
909
883
// Start Example 38
@@ -976,14 +950,12 @@ func QueryNullMissingFieldsExamples(t *testing.T, db *mongo.Database) {
976
950
}
977
951
}
978
952
953
+ // ProjectionExamples contains examples for specifying projections in find operations.
979
954
func ProjectionExamples (t * testing.T , db * mongo.Database ) {
980
- err := db .RunCommand (
981
- context .Background (),
982
- bson.D {{"dropDatabase" , 1 }},
983
- ).Err ()
984
- require .NoError (t , err )
955
+ coll := db .Collection ("inventory_project" )
985
956
986
- coll := db .Collection ("inventory" )
957
+ err := coll .Drop (context .Background ())
958
+ require .NoError (t , err )
987
959
988
960
{
989
961
// Start Example 42
@@ -1115,7 +1087,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
1115
1087
doc := bsonx.Doc {}
1116
1088
for cursor .Next (context .Background ()) {
1117
1089
doc = doc [:0 ]
1118
- err := cursor .Decode (doc )
1090
+ err := cursor .Decode (& doc )
1119
1091
require .NoError (t , err )
1120
1092
1121
1093
require .True (t , containsKey (doc , "_id" ))
@@ -1152,7 +1124,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
1152
1124
doc := bsonx.Doc {}
1153
1125
for cursor .Next (context .Background ()) {
1154
1126
doc = doc [:0 ]
1155
- err := cursor .Decode (doc )
1127
+ err := cursor .Decode (& doc )
1156
1128
require .NoError (t , err )
1157
1129
1158
1130
require .False (t , containsKey (doc , "_id" ))
@@ -1188,7 +1160,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
1188
1160
doc := bsonx.Doc {}
1189
1161
for cursor .Next (context .Background ()) {
1190
1162
doc = doc [:0 ]
1191
- err := cursor .Decode (doc )
1163
+ err := cursor .Decode (& doc )
1192
1164
require .NoError (t , err )
1193
1165
1194
1166
require .True (t , containsKey (doc , "_id" ))
@@ -1225,7 +1197,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
1225
1197
doc := bsonx.Doc {}
1226
1198
for cursor .Next (context .Background ()) {
1227
1199
doc = doc [:0 ]
1228
- err := cursor .Decode (doc )
1200
+ err := cursor .Decode (& doc )
1229
1201
require .NoError (t , err )
1230
1202
1231
1203
require .True (t , containsKey (doc , "_id" ))
@@ -1234,9 +1206,9 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
1234
1206
require .True (t , containsKey (doc , "size" ))
1235
1207
require .False (t , containsKey (doc , "instock" ))
1236
1208
1237
- require .True (t , containsKey (doc , "uom " , "size " ))
1238
- require .False (t , containsKey (doc , "h " , "size " ))
1239
- require .False (t , containsKey (doc , "w " , "size " ))
1209
+ require .True (t , containsKey (doc , "size " , "uom " ))
1210
+ require .False (t , containsKey (doc , "size " , "h " ))
1211
+ require .False (t , containsKey (doc , "size " , "w " ))
1240
1212
1241
1213
}
1242
1214
@@ -1265,7 +1237,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
1265
1237
doc := bsonx.Doc {}
1266
1238
for cursor .Next (context .Background ()) {
1267
1239
doc = doc [:0 ]
1268
- err := cursor .Decode (doc )
1240
+ err := cursor .Decode (& doc )
1269
1241
require .NoError (t , err )
1270
1242
1271
1243
require .True (t , containsKey (doc , "_id" ))
@@ -1274,9 +1246,9 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
1274
1246
require .True (t , containsKey (doc , "size" ))
1275
1247
require .True (t , containsKey (doc , "instock" ))
1276
1248
1277
- require .False (t , containsKey (doc , "uom " , "size " ))
1278
- require .True (t , containsKey (doc , "h " , "size " ))
1279
- require .True (t , containsKey (doc , "w " , "size " ))
1249
+ require .False (t , containsKey (doc , "size " , "uom " ))
1250
+ require .True (t , containsKey (doc , "size " , "h " ))
1251
+ require .True (t , containsKey (doc , "size " , "w " ))
1280
1252
1281
1253
}
1282
1254
@@ -1307,7 +1279,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
1307
1279
doc := bsonx.Doc {}
1308
1280
for cursor .Next (context .Background ()) {
1309
1281
doc = doc [:0 ]
1310
- err := cursor .Decode (doc )
1282
+ err := cursor .Decode (& doc )
1311
1283
require .NoError (t , err )
1312
1284
1313
1285
require .True (t , containsKey (doc , "_id" ))
@@ -1360,7 +1332,7 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
1360
1332
doc := bsonx.Doc {}
1361
1333
for cursor .Next (context .Background ()) {
1362
1334
doc = doc [:0 ]
1363
- err := cursor .Decode (doc )
1335
+ err := cursor .Decode (& doc )
1364
1336
require .NoError (t , err )
1365
1337
1366
1338
require .True (t , containsKey (doc , "_id" ))
@@ -1378,14 +1350,12 @@ func ProjectionExamples(t *testing.T, db *mongo.Database) {
1378
1350
}
1379
1351
}
1380
1352
1353
+ // UpdateExamples contains examples of update operations.
1381
1354
func UpdateExamples (t * testing.T , db * mongo.Database ) {
1382
- err := db .RunCommand (
1383
- context .Background (),
1384
- bson.D {{"dropDatabase" , 1 }},
1385
- ).Err ()
1386
- require .NoError (t , err )
1355
+ coll := db .Collection ("inventory_update" )
1387
1356
1388
- coll := db .Collection ("inventory" )
1357
+ err := coll .Drop (context .Background ())
1358
+ require .NoError (t , err )
1389
1359
1390
1360
{
1391
1361
// Start Example 51
@@ -1537,7 +1507,7 @@ func UpdateExamples(t *testing.T, db *mongo.Database) {
1537
1507
doc := bsonx.Doc {}
1538
1508
for cursor .Next (context .Background ()) {
1539
1509
doc = doc [:0 ]
1540
- err := cursor .Decode (doc )
1510
+ err := cursor .Decode (& doc )
1541
1511
require .NoError (t , err )
1542
1512
1543
1513
uom , err := doc .LookupErr ("size" , "uom" )
@@ -1594,7 +1564,7 @@ func UpdateExamples(t *testing.T, db *mongo.Database) {
1594
1564
doc := bsonx.Doc {}
1595
1565
for cursor .Next (context .Background ()) {
1596
1566
doc = doc [:0 ]
1597
- err := cursor .Decode (doc )
1567
+ err := cursor .Decode (& doc )
1598
1568
require .NoError (t , err )
1599
1569
1600
1570
uom , err := doc .LookupErr ("size" , "uom" )
@@ -1651,7 +1621,7 @@ func UpdateExamples(t *testing.T, db *mongo.Database) {
1651
1621
doc := bsonx.Doc {}
1652
1622
for cursor .Next (context .Background ()) {
1653
1623
doc = doc [:0 ]
1654
- err := cursor .Decode (doc )
1624
+ err := cursor .Decode (& doc )
1655
1625
require .NoError (t , err )
1656
1626
1657
1627
require .True (t , containsKey (doc , "_id" ))
@@ -1669,14 +1639,12 @@ func UpdateExamples(t *testing.T, db *mongo.Database) {
1669
1639
1670
1640
}
1671
1641
1642
+ // DeleteExamples contains examples of delete operations.
1672
1643
func DeleteExamples (t * testing.T , db * mongo.Database ) {
1673
- err := db .RunCommand (
1674
- context .Background (),
1675
- bson.D {{"dropDatabase" , 1 }},
1676
- ).Err ()
1677
- require .NoError (t , err )
1644
+ coll := db .Collection ("inventory_delete" )
1678
1645
1679
- coll := db .Collection ("inventory" )
1646
+ err := coll .Drop (context .Background ())
1647
+ require .NoError (t , err )
1680
1648
1681
1649
{
1682
1650
// Start Example 55
0 commit comments