Skip to content

Commit e82d777

Browse files
author
Divjot Arora
authored
GODRIVER-1377 Database documentation (#215)
1 parent 165f02b commit e82d777

File tree

1 file changed

+65
-18
lines changed

1 file changed

+65
-18
lines changed

mongo/database.go

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
defaultRunCmdOpts = []*options.RunCmdOptions{options.RunCmd().SetReadPreference(readpref.Primary())}
2929
)
3030

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.
3232
type Database struct {
3333
client *Client
3434
name string
@@ -85,7 +85,7 @@ func newDatabase(client *Client, name string, opts ...*options.DatabaseOptions)
8585
return db
8686
}
8787

88-
// Client returns the Client the database was created from.
88+
// Client returns the Client the Database was created from.
8989
func (db *Database) Client() *Client {
9090
return db.client
9191
}
@@ -95,14 +95,21 @@ func (db *Database) Name() string {
9595
return db.name
9696
}
9797

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.
9999
func (db *Database) Collection(name string, opts ...*options.CollectionOptions) *Collection {
100100
return newCollection(db, name, opts...)
101101
}
102102

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.
104105
//
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).
106113
func (db *Database) Aggregate(ctx context.Context, pipeline interface{},
107114
opts ...*options.AggregateOptions) (*Cursor, error) {
108115
a := aggregateParams{
@@ -161,8 +168,13 @@ func (db *Database) processRunCommand(ctx context.Context, cmd interface{},
161168
Database(db.name).Deployment(db.client.deployment).ReadConcern(db.readConcern).Crypt(db.client.crypt), sess, nil
162169
}
163170

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).
166178
func (db *Database) RunCommand(ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) *SingleResult {
167179
if ctx == nil {
168180
ctx = context.Background()
@@ -182,8 +194,15 @@ func (db *Database) RunCommand(ctx context.Context, runCommand interface{}, opts
182194
}
183195
}
184196

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).
187206
func (db *Database) RunCommandCursor(ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) (*Cursor, error) {
188207
if ctx == nil {
189208
ctx = context.Background()
@@ -209,7 +228,8 @@ func (db *Database) RunCommandCursor(ctx context.Context, runCommand interface{}
209228
return cursor, replaceErrors(err)
210229
}
211230

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.
213233
func (db *Database) Drop(ctx context.Context) error {
214234
if ctx == nil {
215235
ctx = context.Background()
@@ -254,7 +274,16 @@ func (db *Database) Drop(ctx context.Context) error {
254274
return nil
255275
}
256276

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).
258287
func (db *Database) ListCollections(ctx context.Context, filter interface{}, opts ...*options.ListCollectionsOptions) (*Cursor, error) {
259288
if ctx == nil {
260289
ctx = context.Background()
@@ -314,7 +343,15 @@ func (db *Database) ListCollections(ctx context.Context, filter interface{}, opt
314343
return cursor, replaceErrors(err)
315344
}
316345

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).
318355
func (db *Database) ListCollectionNames(ctx context.Context, filter interface{}, opts ...*options.ListCollectionsOptions) ([]string, error) {
319356
opts = append(opts, options.ListCollections().SetNameOnly(true))
320357

@@ -350,24 +387,34 @@ func (db *Database) ListCollectionNames(ctx context.Context, filter interface{},
350387
return names, nil
351388
}
352389

353-
// ReadConcern returns the read concern of this database.
390+
// ReadConcern returns the read concern used to configure the Database object.
354391
func (db *Database) ReadConcern() *readconcern.ReadConcern {
355392
return db.readConcern
356393
}
357394

358-
// ReadPreference returns the read preference of this database.
395+
// ReadPreference returns the read preference used to configure the Database object.
359396
func (db *Database) ReadPreference() *readpref.ReadPref {
360397
return db.readPreference
361398
}
362399

363-
// WriteConcern returns the write concern of this database.
400+
// WriteConcern returns the write concern used to configure the Database object.
364401
func (db *Database) WriteConcern() *writeconcern.WriteConcern {
365402
return db.writeConcern
366403
}
367404

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).
371418
func (db *Database) Watch(ctx context.Context, pipeline interface{},
372419
opts ...*options.ChangeStreamOptions) (*ChangeStream, error) {
373420

0 commit comments

Comments
 (0)