Skip to content

Commit 3f37fd6

Browse files
committed
Update documentation for mongo package doc.go file
GODRIVER-570 GODRIVER-571 Change-Id: Ia4ff155ed93f6e2f3418c7dd06b6532ce2637984
1 parent d685dac commit 3f37fd6

File tree

4 files changed

+81
-125
lines changed

4 files changed

+81
-125
lines changed

mongo/client_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package mongo
2+
3+
import (
4+
"context"
5+
"time"
6+
)
7+
8+
func ExampleClient_Connect() {
9+
client, err := NewClient("mongodb://foo:bar@localhost:27017")
10+
if err != nil {
11+
return
12+
}
13+
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
14+
defer cancel()
15+
err = client.Connect(ctx)
16+
if err != nil {
17+
return
18+
}
19+
20+
return
21+
}

mongo/collection.go

Lines changed: 20 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ func (coll *Collection) Database() *Database {
142142
return coll.db
143143
}
144144

145-
// BulkWrite performs a bulk write operation. A custom context can be supplied to this method or nil to default to
146-
// context.Background().
145+
// BulkWrite performs a bulk write operation.
146+
//
147+
// See https://docs.mongodb.com/manual/core/bulk-write-operations/.
147148
func (coll *Collection) BulkWrite(ctx context.Context, models []WriteModel,
148149
opts ...*options.BulkWriteOptions) (*BulkWriteResult, error) {
149150

@@ -204,15 +205,7 @@ func (coll *Collection) BulkWrite(ctx context.Context, models []WriteModel,
204205
}, nil
205206
}
206207

207-
// InsertOne inserts a single document into the collection. A user can supply
208-
// a custom context to this method, or nil to default to context.Background().
209-
//
210-
// This method uses TransformDocument to turn the document parameter into a
211-
// *bsonx.Document. See TransformDocument for the list of valid types for
212-
// document.
213-
//
214-
// TODO(skriptble): Determine if we should unwrap the value for the
215-
// InsertOneResult or just return the bsonx.Element or a bsonx.Value.
208+
// InsertOne inserts a single document into the collection.
216209
func (coll *Collection) InsertOne(ctx context.Context, document interface{},
217210
opts ...*options.InsertOneOptions) (*InsertOneResult, error) {
218211

@@ -272,16 +265,7 @@ func (coll *Collection) InsertOne(ctx context.Context, document interface{},
272265
return &InsertOneResult{InsertedID: insertedID}, err
273266
}
274267

275-
// InsertMany inserts the provided documents. A user can supply a custom context to this
276-
// method.
277-
//
278-
// Currently, batching is not implemented for this operation. Because of this, extremely large
279-
// sets of documents will not fit into a single BSON document to be sent to the server, so the
280-
// operation will fail.
281-
//
282-
// This method uses TransformDocument to turn the documents parameter into a
283-
// *bsonx.Document. See TransformDocument for the list of valid types for
284-
// documents.
268+
// InsertMany inserts the provided documents.
285269
func (coll *Collection) InsertMany(ctx context.Context, documents []interface{},
286270
opts ...*options.InsertManyOptions) (*InsertManyResult, error) {
287271

@@ -363,12 +347,7 @@ func (coll *Collection) InsertMany(ctx context.Context, documents []interface{},
363347
return &InsertManyResult{InsertedIDs: result}, err
364348
}
365349

366-
// DeleteOne deletes a single document from the collection. A user can supply
367-
// a custom context to this method, or nil to default to context.Background().
368-
//
369-
// This method uses TransformDocument to turn the filter parameter into a
370-
// *bsonx.Document. See TransformDocument for the list of valid types for
371-
// filter.
350+
// DeleteOne deletes a single document from the collection.
372351
func (coll *Collection) DeleteOne(ctx context.Context, filter interface{},
373352
opts ...*options.DeleteOptions) (*DeleteResult, error) {
374353

@@ -425,13 +404,7 @@ func (coll *Collection) DeleteOne(ctx context.Context, filter interface{},
425404
return &DeleteResult{DeletedCount: int64(res.N)}, err
426405
}
427406

428-
// DeleteMany deletes multiple documents from the collection. A user can
429-
// supply a custom context to this method, or nil to default to
430-
// context.Background().
431-
//
432-
// This method uses TransformDocument to turn the filter parameter into a
433-
// *bsonx.Document. See TransformDocument for the list of valid types for
434-
// filter.
407+
// DeleteMany deletes multiple documents from the collection.
435408
func (coll *Collection) DeleteMany(ctx context.Context, filter interface{},
436409
opts ...*options.DeleteOptions) (*DeleteResult, error) {
437410

@@ -542,12 +515,7 @@ func (coll *Collection) updateOrReplaceOne(ctx context.Context, filter,
542515
return res, err
543516
}
544517

545-
// UpdateOne updates a single document in the collection. A user can supply a
546-
// custom context to this method, or nil to default to context.Background().
547-
//
548-
// This method uses TransformDocument to turn the filter and update parameter
549-
// into a *bsonx.Document. See TransformDocument for the list of valid types for
550-
// filter and update.
518+
// UpdateOne updates a single document in the collection.
551519
func (coll *Collection) UpdateOne(ctx context.Context, filter interface{}, update interface{},
552520
opts ...*options.UpdateOptions) (*UpdateResult, error) {
553521

@@ -579,12 +547,7 @@ func (coll *Collection) UpdateOne(ctx context.Context, filter interface{}, updat
579547
return coll.updateOrReplaceOne(ctx, f, u, sess, opts...)
580548
}
581549

582-
// UpdateMany updates multiple documents in the collection. A user can supply
583-
// a custom context to this method, or nil to default to context.Background().
584-
//
585-
// This method uses TransformDocument to turn the filter and update parameter
586-
// into a *bsonx.Document. See TransformDocument for the list of valid types for
587-
// filter and update.
550+
// UpdateMany updates multiple documents in the collection.
588551
func (coll *Collection) UpdateMany(ctx context.Context, filter interface{}, update interface{},
589552
opts ...*options.UpdateOptions) (*UpdateResult, error) {
590553

@@ -664,12 +627,7 @@ func (coll *Collection) UpdateMany(ctx context.Context, filter interface{}, upda
664627
return res, err
665628
}
666629

667-
// ReplaceOne replaces a single document in the collection. A user can supply
668-
// a custom context to this method, or nil to default to context.Background().
669-
//
670-
// This method uses TransformDocument to turn the filter and replacement
671-
// parameter into a *bsonx.Document. See TransformDocument for the list of
672-
// valid types for filter and replacement.
630+
// ReplaceOne replaces a single document in the collection.
673631
func (coll *Collection) ReplaceOne(ctx context.Context, filter interface{},
674632
replacement interface{}, opts ...*options.ReplaceOptions) (*UpdateResult, error) {
675633

@@ -710,14 +668,9 @@ func (coll *Collection) ReplaceOne(ctx context.Context, filter interface{},
710668
return coll.updateOrReplaceOne(ctx, f, r, sess, updateOptions...)
711669
}
712670

713-
// Aggregate runs an aggregation framework pipeline. A user can supply a custom context to
714-
// this method.
671+
// Aggregate runs an aggregation framework pipeline.
715672
//
716673
// See https://docs.mongodb.com/manual/aggregation/.
717-
//
718-
// This method uses TransformDocument to turn the pipeline parameter into a
719-
// *bsonx.Document. See TransformDocument for the list of valid types for
720-
// pipeline.
721674
func (coll *Collection) Aggregate(ctx context.Context, pipeline interface{},
722675
opts ...*options.AggregateOptions) (Cursor, error) {
723676

@@ -774,12 +727,7 @@ func (coll *Collection) Aggregate(ctx context.Context, pipeline interface{},
774727
return cursor, replaceTopologyErr(err)
775728
}
776729

777-
// Count gets the number of documents matching the filter. A user can supply a
778-
// custom context to this method, or nil to default to context.Background().
779-
//
780-
// This method uses TransformDocument to turn the filter parameter into a
781-
// *bsonx.Document. See TransformDocument for the list of valid types for
782-
// filter.
730+
// Count gets the number of documents matching the filter.
783731
func (coll *Collection) Count(ctx context.Context, filter interface{},
784732
opts ...*options.CountOptions) (int64, error) {
785733

@@ -827,11 +775,7 @@ func (coll *Collection) Count(ctx context.Context, filter interface{},
827775
return count, replaceTopologyErr(err)
828776
}
829777

830-
// CountDocuments gets the number of documents matching the filter. A user can supply a
831-
// custom context to this method, or nil to default to context.Background().
832-
//
833-
// This method uses countDocumentsAggregatePipeline to turn the filter parameter and options
834-
// into aggregate pipeline.
778+
// CountDocuments gets the number of documents matching the filter.
835779
func (coll *Collection) CountDocuments(ctx context.Context, filter interface{},
836780
opts ...*options.CountOptions) (int64, error) {
837781

@@ -930,12 +874,7 @@ func (coll *Collection) EstimatedDocumentCount(ctx context.Context,
930874
}
931875

932876
// Distinct finds the distinct values for a specified field across a single
933-
// collection. A user can supply a custom context to this method, or nil to
934-
// default to context.Background().
935-
//
936-
// This method uses TransformDocument to turn the filter parameter into a
937-
// *bsonx.Document. See TransformDocument for the list of valid types for
938-
// filter.
877+
// collection.
939878
func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter interface{},
940879
opts ...*options.DistinctOptions) ([]interface{}, error) {
941880

@@ -990,12 +929,7 @@ func (coll *Collection) Distinct(ctx context.Context, fieldName string, filter i
990929
return res.Values, nil
991930
}
992931

993-
// Find finds the documents matching a model. A user can supply a custom context to this
994-
// method.
995-
//
996-
// This method uses TransformDocument to turn the filter parameter into a
997-
// *bsonx.Document. See TransformDocument for the list of valid types for
998-
// filter.
932+
// Find finds the documents matching a model.
999933
func (coll *Collection) Find(ctx context.Context, filter interface{},
1000934
opts ...*options.FindOptions) (Cursor, error) {
1001935

@@ -1047,13 +981,7 @@ func (coll *Collection) Find(ctx context.Context, filter interface{},
1047981
return cursor, replaceTopologyErr(err)
1048982
}
1049983

1050-
// FindOne returns up to one document that matches the model. A user can
1051-
// supply a custom context to this method, or nil to default to
1052-
// context.Background().
1053-
//
1054-
// This method uses TransformDocument to turn the filter parameter into a
1055-
// *bsonx.Document. See TransformDocument for the list of valid types for
1056-
// filter.
984+
// FindOne returns up to one document that matches the model.
1057985
func (coll *Collection) FindOne(ctx context.Context, filter interface{},
1058986
opts ...*options.FindOneOptions) *DocumentResult {
1059987

@@ -1132,14 +1060,7 @@ func (coll *Collection) FindOne(ctx context.Context, filter interface{},
11321060
}
11331061

11341062
// FindOneAndDelete find a single document and deletes it, returning the
1135-
// original in result. The document to return may be nil.
1136-
//
1137-
// A user can supply a custom context to this method, or nil to default to
1138-
// context.Background().
1139-
//
1140-
// This method uses TransformDocument to turn the filter parameter into a
1141-
// *bsonx.Document. See TransformDocument for the list of valid types for
1142-
// filter.
1063+
// original in result.
11431064
func (coll *Collection) FindOneAndDelete(ctx context.Context, filter interface{},
11441065
opts ...*options.FindOneAndDeleteOptions) *DocumentResult {
11451066

@@ -1195,14 +1116,7 @@ func (coll *Collection) FindOneAndDelete(ctx context.Context, filter interface{}
11951116
}
11961117

11971118
// FindOneAndReplace finds a single document and replaces it, returning either
1198-
// the original or the replaced document. The document to return may be nil.
1199-
//
1200-
// A user can supply a custom context to this method, or nil to default to
1201-
// context.Background().
1202-
//
1203-
// This method uses TransformDocument to turn the filter and replacement
1204-
// parameter into a *bsonx.Document. See TransformDocument for the list of
1205-
// valid types for filter and replacement.
1119+
// the original or the replaced document.
12061120
func (coll *Collection) FindOneAndReplace(ctx context.Context, filter interface{},
12071121
replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *DocumentResult {
12081122

@@ -1264,14 +1178,7 @@ func (coll *Collection) FindOneAndReplace(ctx context.Context, filter interface{
12641178
}
12651179

12661180
// FindOneAndUpdate finds a single document and updates it, returning either
1267-
// the original or the updated. The document to return may be nil.
1268-
//
1269-
// A user can supply a custom context to this method, or nil to default to
1270-
// context.Background().
1271-
//
1272-
// This method uses TransformDocument to turn the filter and update parameter
1273-
// into a *bsonx.Document. See TransformDocument for the list of valid types for
1274-
// filter and update.
1181+
// the original or the updated.
12751182
func (coll *Collection) FindOneAndUpdate(ctx context.Context, filter interface{},
12761183
update interface{}, opts ...*options.FindOneAndUpdateOptions) *DocumentResult {
12771184

@@ -1333,6 +1240,7 @@ func (coll *Collection) FindOneAndUpdate(ctx context.Context, filter interface{}
13331240
}
13341241

13351242
// Watch returns a change stream cursor used to receive notifications of changes to the collection.
1243+
//
13361244
// This method is preferred to running a raw aggregation with a $changeStream stage because it
13371245
// supports resumability in the case of some errors.
13381246
func (coll *Collection) Watch(ctx context.Context, pipeline interface{},

mongo/database_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package mongo
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/mongodb/mongo-go-driver/bson"
8+
)
9+
10+
// Individual commands can be sent to the server and response retrieved via run command.
11+
func ExampleDatabase_RunCommand() {
12+
var db *Database
13+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
14+
defer cancel()
15+
_, err := db.RunCommand(ctx, bson.D{{"ping", 1}})
16+
if err != nil {
17+
return
18+
}
19+
return
20+
}

mongo/doc.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
// not use this file except in compliance with the License. You may obtain
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66

7+
// NOTE: This documentation should be kept in line with the Example* test functions.
8+
79
// Package mongo provides a MongoDB Driver API for Go.
810
//
911
// Basic usage of the driver starts with creating a Client from a connection
1012
// string. To do so, call the NewClient and Connect functions:
1113
//
12-
// client, err := mongo.NewClient("mongodb://foo:bar@localhost:27017")
13-
// if err != nil { log.Fatal(err) }
14-
// err = client.Connect(context.TODO())
15-
// if err != nil { log.Fatal(err) }
14+
// client, err := NewClient("mongodb://foo:bar@localhost:27017")
15+
// if err != nil { return err }
16+
// ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
17+
// defer cancel()
18+
// err = client.Connect(ctx)
19+
// if err != nil { return err }
1620
//
1721
// This will create a new client and start monitoring the MongoDB server on localhost.
1822
// The Database and Collection types can be used to access the database:
@@ -21,8 +25,8 @@
2125
//
2226
// A Collection can be used to query the database or insert documents:
2327
//
24-
// res, err := collection.InsertOne(context.Background(), map[string]string{"hello": "world"})
25-
// if err != nil { log.Fatal(err) }
28+
// res, err := collection.InsertOne(context.Background(), bson.M{"hello": "world"}))
29+
// if err != nil { return err }
2630
// id := res.InsertedID
2731
//
2832
// Several methods return a cursor, which can be used like this:
@@ -31,22 +35,25 @@
3135
// if err != nil { log.Fatal(err) }
3236
// defer cur.Close(context.Background())
3337
// for cur.Next(context.Background()) {
34-
// elem := bson.NewDocument()
35-
// err := cur.Decode(elem)
38+
// raw, err := cur.DecodeBytes()
3639
// if err != nil { log.Fatal(err) }
3740
// // do something with elem....
3841
// }
3942
// if err := cur.Err(); err != nil {
40-
// log.Fatal(err)
43+
// return err
4144
// }
4245
//
4346
// Methods that only return a single document will return a *DocumentResult, which works
4447
// like a *sql.Row:
4548
//
46-
// result := bson.NewDocument()
47-
// filter := bson.NewDocument(bson.EC.String("hello", "world"))
48-
// err := collection.FindOne(context.Background(), filter).Decode(result)
49-
// if err != nil { log.Fatal(err) }
49+
// result := struct{
50+
// Foo string
51+
// Bar int32
52+
// }{}
53+
// result := bson.Raw{}
54+
// filter := bson.D{{"hello", "world"}}
55+
// err := collection.FindOne(context.Background(), filter).Decode(&result))
56+
// if err != nil { return err }
5057
// // do something with result...
5158
//
5259
// Additional examples can be found under the examples directory in the driver's repository and

0 commit comments

Comments
 (0)