Skip to content

Commit 72b1ea7

Browse files
committed
GODRIVER-1937 Update legacy ListCollections to support the BatchSize option for server version 2.6
1 parent b1d1e30 commit 72b1ea7

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

mongo/integration/database_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,30 @@ func TestDatabase(t *testing.T) {
237237
assert.Nil(mt, err, "expected command %s to contain key 'batchSize'", evt.Command)
238238
})
239239

240-
// The BatchSize option is not honored for ListCollections operations on server version 2.6 due to an
241-
// inconsistency in the legacy OP_QUERY code path (GODRIVER-1937).
242-
cmdMonitoringMtOpts := mtest.NewOptions().MinServerVersion("3.0")
240+
cmdMonitoringMtOpts := mtest.NewOptions().MinServerVersion("2.6")
243241
mt.RunOpts("getMore commands are monitored", cmdMonitoringMtOpts, func(mt *mtest.T) {
244242
createCollections(mt, 2)
245-
assertGetMoreCommandsAreMonitored(mt, "listCollections", func() (*mongo.Cursor, error) {
243+
244+
// For server versions below 3.0, we internally execute ListCollections() as a legacy OP_QUERY against the system.namespaces
245+
// collection. Command monitoring upconversions translate this to a "find" command rather than "listCollections".
246+
cmdName := "listCollections"
247+
if mtest.CompareServerVersions(mtest.ServerVersion(), "3.0") < 0 {
248+
cmdName = "find"
249+
}
250+
assertGetMoreCommandsAreMonitored(mt, cmdName, func() (*mongo.Cursor, error) {
246251
return mt.DB.ListCollections(mtest.Background, bson.D{}, options.ListCollections().SetBatchSize(2))
247252
})
248253
})
249254
mt.RunOpts("killCursors commands are monitored", cmdMonitoringMtOpts, func(mt *mtest.T) {
250255
createCollections(mt, 2)
251-
assertKillCursorsCommandsAreMonitored(mt, "listCollections", func() (*mongo.Cursor, error) {
256+
257+
// For server versions below 3.0, we internally execute ListCollections() as a legacy OP_QUERY against the system.namespaces
258+
// collection. Command monitoring upconversions translate this to a "find" command rather than "listCollections".
259+
cmdName := "listCollections"
260+
if mtest.CompareServerVersions(mtest.ServerVersion(), "3.0") < 0 {
261+
cmdName = "find"
262+
}
263+
assertKillCursorsCommandsAreMonitored(mt, cmdName, func() (*mongo.Cursor, error) {
252264
return mt.DB.ListCollections(mtest.Background, bson.D{}, options.ListCollections().SetBatchSize(2))
253265
})
254266
})

x/mongo/driver/operation_legacy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,12 +459,17 @@ func (op Operation) createLegacyListCollectionsWiremessage(dst []byte, desc desc
459459
optsElems = bsoncore.AppendDocumentElement(optsElems, "$readPreference", rp)
460460
}
461461

462+
var batchSize int32
463+
if val, ok := cmdDoc.Lookup("cursor", "batchSize").AsInt32OK(); ok {
464+
batchSize = val
465+
}
466+
462467
var wmIdx int32
463468
wmIdx, dst = wiremessage.AppendHeaderStart(dst, info.requestID, 0, wiremessage.OpQuery)
464469
dst = wiremessage.AppendQueryFlags(dst, op.slaveOK(desc))
465470
dst = wiremessage.AppendQueryFullCollectionName(dst, op.getFullCollectionName(listCollectionsNamespace))
466471
dst = wiremessage.AppendQueryNumberToSkip(dst, 0)
467-
dst = wiremessage.AppendQueryNumberToReturn(dst, 0)
472+
dst = wiremessage.AppendQueryNumberToReturn(dst, batchSize)
468473
dst = op.appendLegacyQueryDocument(dst, filter, optsElems)
469474
// leave out returnFieldsSelector because it is optional
470475

0 commit comments

Comments
 (0)