Skip to content

Commit b607a57

Browse files
NoahStappjmikola
andauthored
DRIVERS-1408 - Add guidance on adding _id fields to documents to CRUD spec (#1688)
Co-authored-by: Jeremy Mikola <[email protected]>
1 parent 121db8e commit b607a57

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

source/crud/crud.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,13 @@ database-level aggregation will allow users to receive a cursor from these colle
820820
821821
##### Insert, Update, Replace, Delete, and Bulk Writes
822822
823+
###### Generated identifiers
824+
825+
The insert and bulk insert operations described below MUST generate identifiers for all documents that do not already
826+
have them. These identifiers SHOULD be prepended to the document so they are the first field, in order to prevent the
827+
server from spending time re-ordering the document. If a document already has a user-provided identifier, the driver MAY
828+
re-order the document so the identifier is the first field.
829+
823830
```typescript
824831
interface Collection {
825832

@@ -2481,6 +2488,8 @@ aforementioned allowance in the SemVer spec.
24812488
24822489
- 2024-10-30: Document query limitations in `countDocuments`.
24832490
2491+
- 2024-10-28: Clarified that generated identifiers should be prepended to documents.
2492+
24842493
- 2024-10-01: Add sort option to `replaceOne` and `updateOne`.
24852494
24862495
- 2024-09-12: Specify that explain helpers support maxTimeMS.

source/crud/tests/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,15 @@ that `firstEvent.operationId` is equal to `secondEvent.operationId`. Assert both
737737
To force completion of the `w:0` writes, execute `coll.countDocuments` and expect the returned count is
738738
`maxMessageSizeBytes / maxBsonObjectSize + 1`. This is intended to avoid incomplete writes interfering with other tests
739739
that may use this collection.
740+
741+
### 16. Generated document identifiers are the first field in their document
742+
743+
Construct a `MongoClient` (referred to as `client`) with
744+
[command monitoring](../../command-logging-and-monitoring/command-logging-and-monitoring.md) enabled to observe
745+
CommandStartedEvents. For each of `insertOne`, client `bulkWrite`, and collection `bulkWrite`, do the following:
746+
747+
- Execute the command with a document that does not contain an `_id` field.
748+
- If possible, capture the wire protocol message (referred to as `request`) of the command and assert that the first
749+
field of `request.documents[0]` is `_id`.
750+
- Otherwise, capture the CommandStartedEvent (referred to as `event`) emitted by the command and assert that the first
751+
field of `event.command.documents[0]` is `_id`.

0 commit comments

Comments
 (0)