Skip to content

Commit de03a35

Browse files
committed
Merge branch 'pr/57'
Change-Id: I43df317511acb54e7bc7677751ec51bc3c2e088f
2 parents 1098521 + a3f7d38 commit de03a35

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

core/option/options.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ var (
256256
_ InsertOptioner = (*OptOrdered)(nil)
257257
_ InsertOptioner = (*OptWriteConcern)(nil)
258258
_ ListDatabasesOptioner = OptNameOnly(false)
259+
_ ListCollectionsOptioner = OptNameOnly(false)
259260
_ ListIndexesOptioner = OptBatchSize(0)
260261
_ ReplaceOptioner = (*OptBypassDocumentValidation)(nil)
261262
_ ReplaceOptioner = (*OptCollation)(nil)
@@ -734,4 +735,5 @@ func (opt OptNameOnly) Option(d *bson.Document) error {
734735
return nil
735736
}
736737

737-
func (OptNameOnly) listDatabasesOption() {}
738+
func (OptNameOnly) listDatabasesOption() {}
739+
func (OptNameOnly) listCollectionsOption() {}

mongo/database.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/mongodb/mongo-go-driver/core/command"
1414
"github.com/mongodb/mongo-go-driver/core/description"
1515
"github.com/mongodb/mongo-go-driver/core/dispatch"
16+
"github.com/mongodb/mongo-go-driver/core/option"
1617
"github.com/mongodb/mongo-go-driver/core/readconcern"
1718
"github.com/mongodb/mongo-go-driver/core/readpref"
1819
"github.com/mongodb/mongo-go-driver/core/writeconcern"
@@ -90,3 +91,21 @@ func (db *Database) Drop(ctx context.Context) error {
9091
}
9192
return nil
9293
}
94+
95+
// ListCollections list collections from mongodb database.
96+
func (db *Database) ListCollections(ctx context.Context, filter *bson.Document, options ...option.ListCollectionsOptioner) (command.Cursor, error) {
97+
if ctx == nil {
98+
ctx = context.Background()
99+
}
100+
cmd := command.ListCollections{
101+
DB: db.name,
102+
Filter: filter,
103+
Opts: options,
104+
}
105+
cursor, err := dispatch.ListCollections(ctx, cmd, db.client.topology, db.readSelector)
106+
if err != nil && !command.IsNotFound(err) {
107+
return nil, err
108+
}
109+
return cursor, nil
110+
111+
}

mongo/database_internal_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/mongodb/mongo-go-driver/bson"
14+
"github.com/mongodb/mongo-go-driver/core/option"
1415
"github.com/mongodb/mongo-go-driver/internal/testutil"
1516
"github.com/stretchr/testify/require"
1617
)
@@ -70,3 +71,79 @@ func TestDatabase_Drop(t *testing.T) {
7071
require.NotContains(t, list, name)
7172

7273
}
74+
75+
func TestDatabase_ListCollections(t *testing.T) {
76+
t.Parallel()
77+
dbName := "db_list_collection"
78+
db := createTestDatabase(t, &dbName)
79+
collName := "list_collections_name"
80+
coll := db.Collection(collName)
81+
require.Equal(t, coll.Name(), collName)
82+
require.NotNil(t, coll)
83+
cursor, err := db.ListCollections(context.Background(), nil)
84+
require.NoError(t, err)
85+
86+
next := bson.NewDocument()
87+
88+
for cursor.Next(context.Background()) {
89+
err = cursor.Decode(next)
90+
require.NoError(t, err)
91+
92+
elem, err := next.LookupErr("name")
93+
require.NoError(t, err)
94+
if elem.Type() != bson.TypeString {
95+
t.Errorf("Incorrect type for 'name'. got %v; want %v", elem.Type(), bson.TypeString)
96+
t.FailNow()
97+
}
98+
if elem.StringValue() != collName {
99+
t.Errorf("Incorrect collection name. got %s: want %s", elem.StringValue(), collName)
100+
t.FailNow()
101+
}
102+
//Because we run it without nameOnly parameter we should check if another parameter is exist
103+
docType, err := next.LookupErr("type")
104+
require.NoError(t, err)
105+
if docType.StringValue() != "collections" {
106+
t.Errorf("Incorrect cursor type. got %s: want %s", docType.StringValue(), "collections")
107+
t.FailNow()
108+
}
109+
}
110+
defer func() {
111+
err := db.Drop(context.Background())
112+
require.NoError(t, err)
113+
}()
114+
}
115+
116+
func TestDatabase_ListCollectionsOptions(t *testing.T) {
117+
t.Parallel()
118+
dbName := "db_list_collection_options"
119+
db := createTestDatabase(t, &dbName)
120+
collName := "list_collections_options"
121+
coll := db.Collection(collName)
122+
require.Equal(t, coll.Name(), collName)
123+
require.NotNil(t, coll)
124+
cursor, err := db.ListCollections(context.Background(), nil, option.OptNameOnly(true))
125+
require.NoError(t, err)
126+
127+
next := bson.NewDocument()
128+
129+
for cursor.Next(context.Background()) {
130+
err = cursor.Decode(next)
131+
require.NoError(t, err)
132+
133+
elem, err := next.LookupErr("name")
134+
require.NoError(t, err)
135+
136+
if elem.StringValue() != collName {
137+
t.Errorf("Incorrect collection name. got %s: want %s", elem.StringValue(), collName)
138+
t.FailNow()
139+
}
140+
141+
//Because we run it with name only parameter we should check that there are no other parameters
142+
_, err = next.LookupErr("type")
143+
require.Error(t, err)
144+
}
145+
defer func() {
146+
err := db.Drop(context.Background())
147+
require.NoError(t, err)
148+
}()
149+
}

0 commit comments

Comments
 (0)