@@ -176,18 +176,20 @@ interface Collection {
176
176
/**
177
177
* Finds the documents matching the model.
178
178
*
179
- * Note: The filter parameter below equates to the $query meta operator. It cannot
180
- * contain other meta operators like $maxScan. However, do not validate this document
181
- * as it would be impossible to be forwards and backwards compatible. Let the server
182
- * handle the validation.
183
- *
184
179
* Note: result iteration should be backed by a cursor. Depending on the implementation,
185
180
* the cursor may back the returned Iterable instance or an iterator that it produces.
186
181
*
187
- * @see https://www.mongodb.com/docs/manual/core/read-operations-introduction /
182
+ * @see https://www.mongodb.com/docs/manual/reference/command/find /
188
183
*/
189
184
find(filter: Document, options: Optional<FindOptions>): Iterable<Document>;
190
185
186
+ /**
187
+ * Find a document matching the model.
188
+ *
189
+ * @see https://www.mongodb.com/docs/manual/reference/command/find/
190
+ */
191
+ findOne(filter: Document, options: Optional<FindOneOptions>): Optional<Document>;
192
+
191
193
}
192
194
193
195
interface Database {
@@ -712,6 +714,8 @@ class FindOptions {
712
714
*/
713
715
let: Optional<Document>;
714
716
}
717
+
718
+ type FindOneOptions = Omit<FindOptions, 'batchSize' | 'cursorType' | 'limit' | 'noCursorTimeout'>;
715
719
` ` `
716
720
717
721
##### Count API Details
@@ -783,7 +787,26 @@ filter, skip, and limit are added as options to the `aggregate` command.
783
787
In the event this aggregation is run against an empty collection, an empty array will be returned with no ` n ` field.
784
788
Drivers MUST interpret this result as a ` 0 ` count.
785
789
786
- ##### Combining Limit and Batch Size for the Wire Protocol
790
+ ##### findOne API details
791
+
792
+ The ` findOne ` operation is implemented using a ` find ` operation, but only returns the first document returned in the
793
+ cursor. Due to the special case, ` findOne ` does not support the following options supported in ` find ` :
794
+
795
+ - ` batchSize ` : drivers MUST NOT set a ` batchSize ` in the ` find ` command created for a ` findOne ` operation
796
+ - ` cursorType ` : ` findOne ` only supports non-tailable cursors
797
+ - ` limit ` : drivers MUST set ` limit ` to 1 in the ` find ` command created for a ` findOne ` operation
798
+ - ` noCursorTimeout ` : with a ` limit ` of 1 and no ` batchSize ` , there will not be an open cursor on the server
799
+
800
+ To ensure that the cursor is closed regardless of the default server ` batchSize ` , drivers MUST also set
801
+ ` singleBatch : true ` in the ` find ` command.
802
+
803
+ ##### Setting limit and batchSize options for find commands
804
+
805
+ When users specify both ` limit ` and ` batchSize ` options with the same value, the server returns all results in the first
806
+ batch, but still leaves an open cursor that needs to be closed using the ` killCursors ` command. To avoid this, drivers
807
+ MUST send a value of ` limit + 1 ` for ` batchSize ` in the resulting ` find ` command. This eliminates the open cursor issue.
808
+
809
+ ##### Combining Limit and Batch Size for OP_QUERY
787
810
788
811
The OP_QUERY wire protocol only contains a numberToReturn value which drivers must calculate to get expected limit and
789
812
batch size behavior. Subsequent calls to OP_GET_MORE should use the user-specified batchSize or default to 0. If the
@@ -2397,12 +2420,6 @@ update and delete. This generally causes some issues for new developers and is a
2397
2420
developers. The safest way to combat this without introducing discrepancies between drivers/driver versions or breaking
2398
2421
backwards compatibility was to use multiple methods, each signifying the number of documents that could be affected.
2399
2422
2400
- Q: Speaking of "One", where is ` findOne ` ?
2401
-
2402
- If your driver wishes to offer a ` findOne ` method, that is perfectly fine. If you choose to implement ` findOne ` , please
2403
- keep to the naming conventions followed by the ` FindOptions ` and keep in mind that certain things don't make sense like
2404
- limit (which should be -1), tailable, awaitData, etc...
2405
-
2406
2423
Q: What considerations have been taken for the eventual merging of query and the aggregation framework?
2407
2424
2408
2425
In the future, it is probable that a new query engine (QE) will look very much like the aggregation framework. Given
@@ -2496,6 +2513,8 @@ aforementioned allowance in the SemVer spec.
2496
2513
2497
2514
## Changelog
2498
2515
2516
+ - 2024-11-13: Define ` findOne ` operation as optional, and add guidance on ` limit ` and ` batchSize ` for ` find ` operations.
2517
+
2499
2518
- 2024-11-04: Always send a value for ` bypassDocumentValidation ` if it was specified.
2500
2519
2501
2520
- 2024-11-01: Add hint to DistinctOptions
0 commit comments