Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't return views in listCollections #626

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions exporter/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
var systemDBs = []string{"admin", "config", "local"} //nolint:gochecknoglobals

func listCollections(ctx context.Context, client *mongo.Client, database string, filterInNamespaces []string) ([]string, error) {
filter := bson.D{} // Default=empty -> list all collections
filter := bson.D{{Key: "kind", Value: "collection"}} // Default = list collections but not views

// if there is a filter with the list of collections we want, create a filter like
// $or: {
Expand All @@ -53,7 +53,7 @@ func listCollections(ctx context.Context, client *mongo.Client, database string,
}

if len(matchExpressions) > 0 {
filter = bson.D{{Key: "$or", Value: matchExpressions}}
filter = bson.D{{Key: "$and", Value: bson.A{filter, bson.D{{Key: "$or", Value: matchExpressions}}}}}
}
}

Expand Down
20 changes: 16 additions & 4 deletions exporter/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ func setupDB(ctx context.Context, t *testing.T, client *mongo.Client) {
_, err := client.Database(dbname).Collection(coll).InsertOne(ctx, bson.M{"f1": j, "f2": "2"})
assert.NoError(t, err)
}
// Create views (to ensure that they are not returned for collstats)
// This will also create the system.views collection
pipeline := mongo.Pipeline{bson.D{
{"$project", bson.D{
{"_id", 0},
{"f3", bson.D{
{"$concat", []string{"f1", " ", "f2"}},
}},
}},
}}
err := client.Database(dbname).CreateView(ctx, "view-"+coll, coll, pipeline)
assert.NoError(t, err)
}
}
}
Expand Down Expand Up @@ -115,7 +127,7 @@ func TestListCollections(t *testing.T) {
t.Run("With namespaces list", func(t *testing.T) {
// Advanced filtering test
wantNS := map[string][]string{
"testdb01": {"col01", "col02", "colxx", "colyy"},
"testdb01": {"col01", "col02", "colxx", "colyy", "system.views"},
"testdb02": {"col01", "col02"},
}
// List all collections in testdb01 (inDBs[0]) but only col01 and col02 from testdb02.
Expand All @@ -127,8 +139,8 @@ func TestListCollections(t *testing.T) {

t.Run("Empty namespaces list", func(t *testing.T) {
wantNS := map[string][]string{
"testdb01": {"col01", "col02", "colxx", "colyy"},
"testdb02": {"col01", "col02", "colxx", "colyy"},
"testdb01": {"col01", "col02", "colxx", "colyy", "system.views"},
"testdb02": {"col01", "col02", "colxx", "colyy", "system.views"},
}
namespaces, err := listAllCollections(ctx, client, nil, systemDBs)
assert.NoError(t, err)
Expand All @@ -138,7 +150,7 @@ func TestListCollections(t *testing.T) {
t.Run("Count basic", func(t *testing.T) {
count, err := nonSystemCollectionsCount(ctx, client, nil, nil)
assert.NoError(t, err)
assert.Equal(t, 8, count)
assert.Equal(t, 10, count)
})

t.Run("Filtered count", func(t *testing.T) {
Expand Down