Skip to content

Commit ac85d3e

Browse files
committed
GODRIVER-568 Add Database accessor to Collection
Change-Id: Id5a0462addd15df659ff0609a18d2bbe3a511476
1 parent 7c7f13e commit ac85d3e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

mongo/collection.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ func (coll *Collection) namespace() command.Namespace {
136136
return command.NewNamespace(coll.db.name, coll.name)
137137
}
138138

139+
// Database provides access to the database that contains the collection.
140+
func (coll *Collection) Database() *Database {
141+
return coll.db
142+
}
143+
139144
// InsertOne inserts a single document into the collection. A user can supply
140145
// a custom context to this method, or nil to default to context.Background().
141146
//

mongo/collection_internal_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ func TestCollection_name_accessor(t *testing.T) {
174174

175175
}
176176

177+
func TestCollection_database_accessor(t *testing.T) {
178+
t.Parallel()
179+
180+
dbName := "foo"
181+
collName := "bar"
182+
183+
coll := createTestCollection(t, &dbName, &collName)
184+
require.Equal(t, coll.Database().Name(), dbName)
185+
}
186+
177187
func TestCollection_InsertOne(t *testing.T) {
178188
if testing.Short() {
179189
t.Skip("skipping integration test in short mode")

0 commit comments

Comments
 (0)