Skip to content

Commit 88b994e

Browse files
authored
Add client-side _id FAQ entry (#1592)
Clarify why _id values are created client-side by default
1 parent dc6eb4c commit 88b994e

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

source/server_write_commands.rst

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,14 +464,25 @@ response would look, had the request asked for that write concern.
464464
FAQ
465465
---
466466

467-
Can a driver still use the OP_INSERT, OP_DELETE, OP_UPDATE?
468-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
467+
Why are ``_id`` values generated client-side by default for new documents?
468+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
469+
470+
Though drivers may expose configuration options to prevent this behavior, by default a new ``ObjectId`` value will be created client-side before an insert or upsert operation.
471+
472+
This design decision primarily stems from the fact that MongoDB is a distributed database and the typical unique auto-incrementing scalar value most RDBMS' use for generating a primary key would not be robust enough, necessitating the need for a more robust data type (``ObjectId`` in this case). These ``_id`` values can be generated either on the client or the server, however when done client-side a new document's ``_id`` value is immediately available for use without the need for a network round trip.
473+
474+
Prior to MongoDB 3.6, an ``insert`` operation would use the ``OP_INSERT`` opcode of the wire protocol to send the operation, and retrieve the results subsequently with a ``getLastError`` command. If client-side ``_id`` values were omitted, this command response wouldn't contain the server-created ``_id`` values for new documents. Following MongoDB 3.6 when all commands would be issued using the ``OP_MSG`` wire protocol opcode (``insert`` included), the response to the command still wouldn't contain the ``_id`` values for inserted documents.
475+
476+
477+
Can a driver still use the ``OP_INSERT``, ``OP_DELETE``, ``OP_UPDATE``?
478+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
469479

470480
Yes, a 2.6 server will still support those. But it is unlikely that a 2.8 server would. Of course, when talking to older servers, the usual op codes will continue working the same. An older server is one that reports ``hello.maxWireVersion`` to be less than 2 or does not include the field.
471481

472482
The rationale here is that we may choose to divert all the write traffic to the new
473483
protocol. (This depends on the having the overhead to issue a batch with one item very low.)
474484

485+
475486
Can an application still issue requests with write concerns {w: 0}?
476487
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
477488

@@ -494,10 +505,11 @@ Yes but as of 2.6 the existing getLastError behavior is supported for backward c
494505
Changelog
495506
---------
496507

497-
:2014-05-14: First public version
508+
:2024-06-04: Add FAQ entry outlining client-side _id value generation
509+
:2022-10-05: Revise spec front matter and reformat changelog.
510+
:2022-07-25: Remove outdated value for ``maxWriteBatchSize``
511+
:2021-04-22: Updated to use hello command
498512
:2014-05-15: Removed text related to bulk operations; see the Bulk API spec for
499513
bulk details. Clarified some paragraphs; re-ordered the response
500514
field sections.
501-
:2021-04-22: Updated to use hello command
502-
:2022-07-25: Remove outdated value for ``maxWriteBatchSize``
503-
:2022-10-05: Revise spec front matter and reformat changelog.
515+
:2014-05-14: First public version

0 commit comments

Comments
 (0)