28
28
defaultRunCmdOpts = []* options.RunCmdOptions {options .RunCmd ().SetReadPreference (readpref .Primary ())}
29
29
)
30
30
31
- // Database performs operations on a given database.
31
+ // Database is a handle to a MongoDB database. It is safe for concurrent use by multiple goroutines .
32
32
type Database struct {
33
33
client * Client
34
34
name string
@@ -85,7 +85,7 @@ func newDatabase(client *Client, name string, opts ...*options.DatabaseOptions)
85
85
return db
86
86
}
87
87
88
- // Client returns the Client the database was created from.
88
+ // Client returns the Client the Database was created from.
89
89
func (db * Database ) Client () * Client {
90
90
return db .client
91
91
}
@@ -95,14 +95,21 @@ func (db *Database) Name() string {
95
95
return db .name
96
96
}
97
97
98
- // Collection gets a handle for a given collection in the database .
98
+ // Collection gets a handle for a collection with the given name configured with the given CollectionOptions .
99
99
func (db * Database ) Collection (name string , opts ... * options.CollectionOptions ) * Collection {
100
100
return newCollection (db , name , opts ... )
101
101
}
102
102
103
- // Aggregate runs an aggregation framework pipeline.
103
+ // Aggregate performs an aggregate operation (https://docs.mongodb.com/manual/reference/command/aggregate/) against
104
+ // the database. This requires MongoDB version >= 3.6 and driver version >= 1.1.0.
104
105
//
105
- // See https://docs.mongodb.com/manual/aggregation/.
106
+ // The pipeline parameter should be a slice of documents, each representing an aggregation stage. The pipeline
107
+ // cannot be nil but can be empty. The stage documents must all be non-nil. For a pipeline of bson.D documents, the
108
+ // mongo.Pipeline type can be used. See
109
+ // https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/#db-aggregate-stages for a list of valid
110
+ // stages in database-level aggregations.
111
+ //
112
+ // The opts parameter can be used to specify options for this operation (see the options.AggregateOptions documentation).
106
113
func (db * Database ) Aggregate (ctx context.Context , pipeline interface {},
107
114
opts ... * options.AggregateOptions ) (* Cursor , error ) {
108
115
a := aggregateParams {
@@ -161,8 +168,13 @@ func (db *Database) processRunCommand(ctx context.Context, cmd interface{},
161
168
Database (db .name ).Deployment (db .client .deployment ).ReadConcern (db .readConcern ).Crypt (db .client .crypt ), sess , nil
162
169
}
163
170
164
- // RunCommand runs a command on the database. A user can supply a custom
165
- // context to this method, or nil to default to context.Background().
171
+ // RunCommand executes the given command against the database.
172
+ //
173
+ // The runCommand parameter should be a document for the command to be executed. It cannot be nil.
174
+ // This must be an order-preserving type such as bson.D. Map types such as bson.M are not valid.
175
+ // If the command document contains a session ID or any transaction-specific fields, the behavior is undefined.
176
+ //
177
+ // The opts parameter can be used to specify options for this operation (see the options.RunCmdOptions documentation).
166
178
func (db * Database ) RunCommand (ctx context.Context , runCommand interface {}, opts ... * options.RunCmdOptions ) * SingleResult {
167
179
if ctx == nil {
168
180
ctx = context .Background ()
@@ -182,8 +194,15 @@ func (db *Database) RunCommand(ctx context.Context, runCommand interface{}, opts
182
194
}
183
195
}
184
196
185
- // RunCommandCursor runs a command on the database and returns a cursor over the resulting reader. A user can supply
186
- // a custom context to this method, or nil to default to context.Background().
197
+ // RunCommandCursor executes the given command against the database and parses the response as a cursor. If the command
198
+ // being executed does not return a cursor (e.g. insert), the command will be executed on the server and an error
199
+ // will be returned because the server response cannot be parsed as a cursor.
200
+ //
201
+ // The runCommand parameter should be a document for the command to be executed. It cannot be nil.
202
+ // This must be an order-preserving type such as bson.D. Map types such as bson.M are not valid.
203
+ // If the command document contains a session ID or any transaction-specific fields, the behavior is undefined.
204
+ //
205
+ // The opts parameter can be used to specify options for this operation (see the options.RunCmdOptions documentation).
187
206
func (db * Database ) RunCommandCursor (ctx context.Context , runCommand interface {}, opts ... * options.RunCmdOptions ) (* Cursor , error ) {
188
207
if ctx == nil {
189
208
ctx = context .Background ()
@@ -209,7 +228,8 @@ func (db *Database) RunCommandCursor(ctx context.Context, runCommand interface{}
209
228
return cursor , replaceErrors (err )
210
229
}
211
230
212
- // Drop drops this database from mongodb.
231
+ // Drop drops the database on the server. This method ignores "namespace not found" errors so it is safe to drop
232
+ // a database that does not exist on the server.
213
233
func (db * Database ) Drop (ctx context.Context ) error {
214
234
if ctx == nil {
215
235
ctx = context .Background ()
@@ -254,7 +274,16 @@ func (db *Database) Drop(ctx context.Context) error {
254
274
return nil
255
275
}
256
276
257
- // ListCollections returns a cursor over the collections in a database.
277
+ // ListCollections performs a listCollections operation
278
+ // (https://docs.mongodb.com/manual/reference/command/listCollections/) and returns a cursor over the collections in
279
+ // the database.
280
+ //
281
+ // The filter parameter should be a document containing query operators and can be used to select which collections
282
+ // are included in the result. It cannot be nil. An empty document (e.g. bson.D{}) should be used to include all
283
+ // collections.
284
+ //
285
+ // The opts parameter can be used to specify options for the operation (see the options.ListCollectionsOptions
286
+ // documentation).
258
287
func (db * Database ) ListCollections (ctx context.Context , filter interface {}, opts ... * options.ListCollectionsOptions ) (* Cursor , error ) {
259
288
if ctx == nil {
260
289
ctx = context .Background ()
@@ -314,7 +343,15 @@ func (db *Database) ListCollections(ctx context.Context, filter interface{}, opt
314
343
return cursor , replaceErrors (err )
315
344
}
316
345
317
- // ListCollectionNames returns a slice containing the names of all of the collections on the server.
346
+ // ListCollectionNames performs a listCollections operation and returns a slice containing the names of the collections
347
+ // in the database. This method requires driver version >= 1.1.0.
348
+ //
349
+ // The filter parameter should be a document containing query operators and can be used to select which collections
350
+ // are included in the result. It cannot be nil. An empty document (e.g. bson.D{}) should be used to include all
351
+ // collections.
352
+ //
353
+ // The opts parameter can be used to specify options for the operation (see the options.ListCollectionsOptions
354
+ // documentation).
318
355
func (db * Database ) ListCollectionNames (ctx context.Context , filter interface {}, opts ... * options.ListCollectionsOptions ) ([]string , error ) {
319
356
opts = append (opts , options .ListCollections ().SetNameOnly (true ))
320
357
@@ -350,24 +387,34 @@ func (db *Database) ListCollectionNames(ctx context.Context, filter interface{},
350
387
return names , nil
351
388
}
352
389
353
- // ReadConcern returns the read concern of this database .
390
+ // ReadConcern returns the read concern used to configure the Database object .
354
391
func (db * Database ) ReadConcern () * readconcern.ReadConcern {
355
392
return db .readConcern
356
393
}
357
394
358
- // ReadPreference returns the read preference of this database .
395
+ // ReadPreference returns the read preference used to configure the Database object .
359
396
func (db * Database ) ReadPreference () * readpref.ReadPref {
360
397
return db .readPreference
361
398
}
362
399
363
- // WriteConcern returns the write concern of this database .
400
+ // WriteConcern returns the write concern used to configure the Database object .
364
401
func (db * Database ) WriteConcern () * writeconcern.WriteConcern {
365
402
return db .writeConcern
366
403
}
367
404
368
- // Watch returns a change stream cursor used to receive information of changes to the database. This method is preferred
369
- // to running a raw aggregation with a $changeStream stage because it supports resumability in the case of some errors.
370
- // The database must have read concern majority or no read concern for a change stream to be created successfully.
405
+ // Watch returns a change stream for all changes to the corresponding database. See
406
+ // https://docs.mongodb.com/manual/changeStreams/ for more information about change streams.
407
+ //
408
+ // The Database must be configured with read concern majority or no read concern for a change stream to be created
409
+ // successfully.
410
+ //
411
+ // The pipeline parameter should be a slice of documents, each representing a pipeline stage. The pipeline cannot be
412
+ // nil but can be empty. The stage documents must all be non-nil. See https://docs.mongodb.com/manual/changeStreams/ for
413
+ // a list of pipeline stages that can be used with change streams. For a pipeline of bson.D documents, the
414
+ // mongo.Pipeline{} type can be used.
415
+ //
416
+ // The opts parameter can be used to specify options for change stream creation (see the options.ChangeStreamOptions
417
+ // documentation).
371
418
func (db * Database ) Watch (ctx context.Context , pipeline interface {},
372
419
opts ... * options.ChangeStreamOptions ) (* ChangeStream , error ) {
373
420
0 commit comments