Most (all?) drivers have a collection.findOne() helper but the CRUD spec only says this:
Q: Speaking of "One", where is findOne?
If your driver wishes to offer a findOne method, that is perfectly fine. If you choose to implement findOne, please keep to the naming conventions followed by the FindOptions and keep in mind that certain things don't make sense like limit (which should be -1), tailable, awaitData, etc...
https://github.com/mongodb/specifications/blob/master/source/crud/crud.md#q--a
We should add tests to ensure that drivers MUST NOT send batchSize=1 with the findOne operation as doing so is unneeded and, at worst, requires the driver to run killCursors. This extra killCursors command is inefficient and adds latency to the findOne(). See SERVER-80713 and SERVER-57067 for more info.
Why am I opening this now?
While looking into HELP-66136 I saw that Node's findOne api is sending both limit=1 and batchSize=1:
async findOne( filter: Filter<TSchema> = {}, options: FindOptions = {} ): Promise<WithId<TSchema> | null> { const cursor = this.find(filter, options).limit(-1).batchSize(1); const res = await cursor.next(); await cursor.close(); return res; }
https://github.com/mongodb/node-mongodb-native/blob/c54466c/src/collection.ts#L506
Note that to reproduce this you may need to set internalQueryFrameworkControl="trySbeEngine":
>>> client.admin.command('buildInfo')['version'] '7.0.8' >>> client.t.command("find", "t", batchSize=1, limit=1) {'cursor': {'firstBatch': [{'_id': ObjectId('670ff88ad508463d0358f622')}], 'id': 0, 'ns': 't.t'}, 'ok': 1.0, ...} >>> client.admin.command({'setParameter': 1, 'internalQueryFrameworkControl': 'trySbeEngine'}) {'was': 'trySbeRestricted', 'ok': 1.0, ...} >>> client.t.command("find", "t", batchSize=1, limit=1) {'cursor': {'firstBatch': [{'_id': ObjectId('670ff88ad508463d0358f622')}], 'id': 5525733552195222418, 'ns': 't.t'}, 'ok': 1.0, ...}
- is duplicated by
-
NODE-6435 Remove `batchSize` as a findOne option and don't set batchSize on the cursor
- Closed
- related to
-
SERVER-82274 singleBatch find on a timeseries collection does not close cursor if batchSize is set
- Closed
-
DRIVERS-958 Add findOne to CRUD
- Closed
- split to
-
CDRIVER-5771 Ensure findOne does not set batchSize=1
- Blocked
-
CSHARP-5376 Ensure findOne does not set batchSize=1
- Blocked
-
CXX-3137 Ensure findOne does not set batchSize=1
- Blocked
-
GODRIVER-3395 Ensure findOne does not set batchSize=1
- Blocked
-
JAVA-5666 Ensure findOne does not set batchSize=1
- Blocked
-
NODE-6471 Ensure findOne does not set batchSize=1
- Blocked
-
PHPLIB-1563 Ensure findOne does not set batchSize=1
- Blocked
-
PYTHON-4920 Ensure findOne does not set batchSize=1
- Blocked
-
RUBY-3567 Ensure findOne does not set batchSize=1
- Blocked
-
RUST-2070 Ensure findOne does not set batchSize=1
- Blocked
-
MOTOR-1387 Ensure findOne does not set batchSize=1
- Closed