Skip to content

Commit dbba268

Browse files
Divjot Aroraskriptble
authored andcommitted
Remove Count from collection.go
GODRIVER-789 Change-Id: If721b5836db716a608a633269f1a07a2a9f1a231
1 parent 3908a1e commit dbba268

File tree

7 files changed

+10
-116
lines changed

7 files changed

+10
-116
lines changed

mongo/causal_consistency_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func createReadFuncMap(t *testing.T, dbName string, collName string) (*Client, *
8282

8383
functions := []CollFunction{
8484
{"Aggregate", coll, nil, func(mctx SessionContext) error { _, err := coll.Aggregate(mctx, emptyArr); return err }},
85-
{"Count", coll, nil, func(mctx SessionContext) error { _, err := coll.Count(mctx, emptyDoc); return err }},
85+
{"EstimatedDocumentCount", coll, nil, func(mctx SessionContext) error { _, err := coll.EstimatedDocumentCount(mctx); return err }},
8686
{"Distinct", coll, nil, func(mctx SessionContext) error { _, err := coll.Distinct(mctx, "field", emptyDoc); return err }},
8787
{"Find", coll, nil, func(mctx SessionContext) error { _, err := coll.Find(mctx, emptyDoc); return err }},
8888
{"FindOne", coll, nil, func(mctx SessionContext) error { res := coll.FindOne(mctx, emptyDoc); return res.err }},

mongo/collection.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -738,54 +738,6 @@ func (coll *Collection) Aggregate(ctx context.Context, pipeline interface{},
738738
return cursor, replaceTopologyErr(err)
739739
}
740740

741-
// Count gets the number of documents matching the filter.
742-
func (coll *Collection) Count(ctx context.Context, filter interface{},
743-
opts ...*options.CountOptions) (int64, error) {
744-
745-
if ctx == nil {
746-
ctx = context.Background()
747-
}
748-
749-
f, err := transformDocument(coll.registry, filter)
750-
if err != nil {
751-
return 0, err
752-
}
753-
754-
sess := sessionFromContext(ctx)
755-
756-
err = coll.client.ValidSession(sess)
757-
if err != nil {
758-
return 0, err
759-
}
760-
761-
rc := coll.readConcern
762-
if sess != nil && (sess.TransactionInProgress()) {
763-
rc = nil
764-
}
765-
766-
oldns := coll.namespace()
767-
cmd := command.Count{
768-
NS: command.Namespace{DB: oldns.DB, Collection: oldns.Collection},
769-
Query: f,
770-
ReadPref: coll.readPreference,
771-
ReadConcern: rc,
772-
Session: sess,
773-
Clock: coll.client.clock,
774-
}
775-
776-
count, err := driver.Count(
777-
ctx, cmd,
778-
coll.client.topology,
779-
coll.readSelector,
780-
coll.client.id,
781-
coll.client.topology.SessionPool,
782-
coll.registry,
783-
opts...,
784-
)
785-
786-
return count, replaceTopologyErr(err)
787-
}
788-
789741
// CountDocuments gets the number of documents matching the filter.
790742
func (coll *Collection) CountDocuments(ctx context.Context, filter interface{},
791743
opts ...*options.CountOptions) (int64, error) {

mongo/collection_internal_test.go

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func TestCollection_ReplaceTopologyError(t *testing.T) {
201201
_, err = coll.Aggregate(context.Background(), pipeline, options.Aggregate())
202202
require.Equal(t, err, ErrClientDisconnected)
203203

204-
_, err = coll.Count(context.Background(), bsonx.Doc{})
204+
_, err = coll.EstimatedDocumentCount(context.Background())
205205
require.Equal(t, err, ErrClientDisconnected)
206206

207207
_, err = coll.CountDocuments(context.Background(), bsonx.Doc{})
@@ -367,9 +367,6 @@ func TestCollection_NilDocumentError(t *testing.T) {
367367
_, err = coll.ReplaceOne(context.Background(), nil, bsonx.Doc{{"_id", bsonx.Double(3.14159)}})
368368
require.Equal(t, err, ErrNilDocument)
369369

370-
_, err = coll.Count(context.Background(), nil)
371-
require.Equal(t, err, ErrNilDocument)
372-
373370
_, err = coll.CountDocuments(context.Background(), nil)
374371
require.Equal(t, err, ErrNilDocument)
375372

@@ -1328,47 +1325,6 @@ func TestCollection_Aggregate_withOptions(t *testing.T) {
13281325
require.NoError(t, err)
13291326
}
13301327

1331-
func TestCollection_Count(t *testing.T) {
1332-
if testing.Short() {
1333-
t.Skip("skipping integration test in short mode")
1334-
}
1335-
1336-
coll := createTestCollection(t, nil, nil)
1337-
initCollection(t, coll)
1338-
1339-
count, err := coll.Count(context.Background(), bsonx.Doc{})
1340-
require.Nil(t, err)
1341-
require.Equal(t, count, int64(5))
1342-
}
1343-
1344-
func TestCollection_Count_withFilter(t *testing.T) {
1345-
if testing.Short() {
1346-
t.Skip("skipping integration test in short mode")
1347-
}
1348-
1349-
coll := createTestCollection(t, nil, nil)
1350-
initCollection(t, coll)
1351-
1352-
filter := bsonx.Doc{{"x", bsonx.Document(bsonx.Doc{{"$gt", bsonx.Int32(2)}})}}
1353-
1354-
count, err := coll.Count(context.Background(), filter)
1355-
require.Nil(t, err)
1356-
require.Equal(t, count, int64(3))
1357-
}
1358-
1359-
func TestCollection_Count_withOption(t *testing.T) {
1360-
if testing.Short() {
1361-
t.Skip("skipping integration test in short mode")
1362-
}
1363-
1364-
coll := createTestCollection(t, nil, nil)
1365-
initCollection(t, coll)
1366-
1367-
count, err := coll.Count(context.Background(), bsonx.Doc{}, options.Count().SetLimit(int64(3)))
1368-
require.Nil(t, err)
1369-
require.Equal(t, count, int64(3))
1370-
}
1371-
13721328
func TestCollection_CountDocuments(t *testing.T) {
13731329
if testing.Short() {
13741330
t.Skip("Skipping integration test in short mode")

mongo/command_monitoring_test.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func runCmTestFile(t *testing.T, filepath string) {
182182
case "updateMany":
183183
cmUpdateManyTest(t, testDoc, operationDoc, coll)
184184
case "count":
185-
cmCountTest(t, testDoc, operationDoc, coll)
185+
// count has been deprecated
186186
case "bulkWrite":
187187
cmBulkWriteTest(t, testDoc, operationDoc, coll)
188188
}
@@ -804,20 +804,6 @@ func cmUpdateManyTest(t *testing.T, testCase bsonx.Doc, operation bsonx.Doc, col
804804
compareExpectations(t, testCase)
805805
}
806806

807-
func cmCountTest(t *testing.T, testCase bsonx.Doc, operation bsonx.Doc, coll *Collection) {
808-
filter := operation.Lookup("arguments").Document().Lookup("filter").Document()
809-
810-
oldRp := coll.readPreference
811-
if rpVal, err := operation.LookupErr("read_preference"); err == nil {
812-
coll.readPreference = getRp(rpVal.Document())
813-
}
814-
815-
_, _ = coll.Count(context.Background(), filter)
816-
coll.readPreference = oldRp
817-
818-
compareExpectations(t, testCase)
819-
}
820-
821807
func cmBulkWriteTest(t *testing.T, testCase bsonx.Doc, operation bsonx.Doc, coll *Collection) {
822808
outerArguments := operation.Lookup("arguments").Document()
823809

mongo/crud_util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ func executeCount(sess *sessionImpl, coll *Collection, args map[string]interface
8282
Context: context.WithValue(ctx, sessionKey{}, sess),
8383
Session: sess,
8484
}
85-
return coll.Count(sessCtx, filter, opts)
85+
return coll.CountDocuments(sessCtx, filter, opts)
8686
}
87-
return coll.Count(ctx, filter, opts)
87+
return coll.CountDocuments(ctx, filter, opts)
8888
}
8989

9090
func executeCountDocuments(sess *sessionImpl, coll *Collection, args map[string]interface{}) (int64, error) {

mongo/sessions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func createFuncMap(t *testing.T, dbName string, collName string, monitored bool)
108108
{"UpdateMany", coll, nil, func(mctx SessionContext) error { _, err := coll.UpdateMany(mctx, emptyDoc, updateDoc); return err }},
109109
{"ReplaceOne", coll, nil, func(mctx SessionContext) error { _, err := coll.ReplaceOne(mctx, emptyDoc, emptyDoc); return err }},
110110
{"Aggregate", coll, nil, func(mctx SessionContext) error { _, err := coll.Aggregate(mctx, emptyArr); return err }},
111-
{"Count", coll, nil, func(mctx SessionContext) error { _, err := coll.Count(mctx, emptyDoc); return err }},
111+
{"EstimatedDocumentCount", coll, nil, func(mctx SessionContext) error { _, err := coll.EstimatedDocumentCount(mctx); return err }},
112112
{"Distinct", coll, nil, func(mctx SessionContext) error { _, err := coll.Distinct(mctx, "field", emptyDoc); return err }},
113113
{"Find", coll, nil, func(mctx SessionContext) error { _, err := coll.Find(mctx, emptyDoc); return err }},
114114
{"FindOne", coll, nil, func(mctx SessionContext) error { res := coll.FindOne(mctx, emptyDoc); return res.err }},

mongo/transactions_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ func runTransactionsTestCase(t *testing.T, test *transTestCase, testfile transTe
218218
}
219219

220220
for _, op := range test.Operations {
221+
if op.Name == "count" {
222+
t.Skip("count has been deprecated")
223+
}
224+
221225
// create collection with default read preference Primary (needed to prevent server selection fail)
222226
coll = db.Collection(collName, options.Collection().SetReadPreference(readpref.Primary()))
223227
addCollectionOptions(coll, op.CollectionOptions)
@@ -388,10 +392,6 @@ func executeSessionOperation(op *transOperation, sess *sessionImpl) error {
388392

389393
func executeCollectionOperation(t *testing.T, op *transOperation, sess *sessionImpl, coll *Collection) error {
390394
switch op.Name {
391-
case "count":
392-
_, err := executeCount(sess, coll, op.ArgMap)
393-
// no results to verify with count
394-
return err
395395
case "countDocuments":
396396
_, err := executeCountDocuments(sess, coll, op.ArgMap)
397397
// no results to verify with count

0 commit comments

Comments
 (0)