Skip to content

Commit beeddbd

Browse files
author
Dave Cuthbert
authored
DOCSP-24028 objectid in mongosh (#2731) (#2847)
* DOCSP-28696 ObjectId in mongosh server updates * DOCSP-28696 ObjectId in mongosh server updates * DOCSP-28696 ObjectId in mongosh server updates * DOCSP-28696 ObjectId in mongosh server updates * DOCSP-28696 ObjectId in mongosh server updates * DOCSP-28696 ObjectId in mongosh server updates * DOCSP-28696 mongosh updates for server ticket * Review feedback * Staging fixes * Staging fixes
1 parent bdabf04 commit beeddbd

File tree

4 files changed

+61
-48
lines changed

4 files changed

+61
-48
lines changed

source/includes/fact-ObjectId-construct.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
- A 3-byte incrementing counter, initialized to a random value.
88

9-
While the BSON format itself is little-endian, the timestamp and
10-
counter values are big-endian, the most significant bytes appear first
11-
in the byte sequence.
9+
For timestamp and counter values, the most significant bytes appear
10+
first in the byte sequence (big-endian). This is unlike other BSON
11+
values, where the least significant bytes appear first (little-endian).
1212

1313
If an integer value is used to create an ObjectId, the integer replaces
1414
the timestamp.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Starting in MongoDB 5.0, :binary:`mongosh` replaces the legacy ``mongo``
2+
shell. The ``ObjectId()`` methods work differently in ``mongosh`` than
3+
in the legacy ``mongo`` shell. For more information on the legacy
4+
methods, see :ref:`Legacy mongo Shell <mongo>`.
5+

source/reference/bson-types.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ benefits:
6868

6969
.. include:: /includes/fact-ObjectId-timestamp-order.rst
7070

71-
.. seealso::
71+
Use the :method:`ObjectId()` methods to set and retrieve ObjectId
72+
values.
73+
74+
.. include:: /includes/reference/fact-objectid-and-mongosh.rst
7275

73-
:method:`ObjectId()`
7476

7577
.. _document-bson-type-string:
7678

source/reference/method/ObjectId.txt

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _server-objectid:
2+
13
==========
24
ObjectId()
35
==========
@@ -28,10 +30,10 @@ Description
2830
.. list-table::
2931
:header-rows: 1
3032
:widths: 20 80
31-
33+
3234
* - Input Type
3335
- Description
34-
36+
3537
* - ``hexadecimal``
3638
- Optional. A 24 character hexadecimal string value for the new
3739
ObjectId.
@@ -41,28 +43,31 @@ Description
4143
:wikipedia:`Unix epoch` to create the new timestamp.
4244

4345

44-
Methods and Attributes
45-
-----------------------
46+
Methods
47+
-------
4648

47-
:method:`ObjectId()` has the following attribute and methods:
49+
:method:`ObjectId()` has the following methods:
4850

4951
.. list-table::
5052
:header-rows: 1
5153
:widths: 30 70
5254

53-
* - Attribute/Method
55+
* - Methods
5456
- Description
55-
56-
* - ``str``
57-
- Returns the hexadecimal string representation of the object.
57+
5858
* - :method:`ObjectId.getTimestamp()`
5959
- Returns the timestamp portion of the object as a Date.
60+
6061
* - :method:`ObjectId.toString()`
61-
- Returns the JavaScript representation in the form of a string
62-
literal "``ObjectId(...)``".
62+
- Returns the ObjectId as a hexadecimal string.
63+
6364
* - :method:`ObjectId.valueOf()`
64-
- Returns the representation of the object as a hexadecimal
65-
string. The returned string is the ``str`` attribute.
65+
- Returns ``ObjectId.self``.
66+
67+
Behavior
68+
--------
69+
70+
.. include:: /includes/reference/fact-objectid-and-mongosh.rst
6671

6772
Examples
6873
--------
@@ -74,40 +79,25 @@ To generate a new ObjectId, use :method:`ObjectId()` with no argument:
7479

7580
.. code-block:: javascript
7681

77-
x = ObjectId()
82+
newObjectId = ObjectId()
7883

79-
In this example, the value of ``x`` is:
84+
In this example, the value of ``newObjectId`` is:
8085

8186
.. code-block:: javascript
8287

8388
ObjectId("507f1f77bcf86cd799439011")
8489

85-
Specify a Hexadecimal String
86-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87-
88-
To generate a new ObjectId using :method:`ObjectId()` with a unique
89-
hexadecimal string:
90-
91-
.. code-block:: javascript
92-
93-
y = ObjectId("507f191e810c19729de860ea")
90+
Return a Hexadecimal String
91+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
9492

95-
In this example, the value of ``y`` would be:
93+
To return the ObjectId as a hexadecimal string, use the ``toString()``
94+
method.
9695

9796
.. code-block:: javascript
9897

99-
ObjectId("507f191e810c19729de860ea")
98+
ObjectId("507f191e810c19729de860ea").toString()
10099

101-
Access the Hexadecimal String
102-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103-
104-
Access the ``str`` attribute of an ``ObjectId()`` object, as follows:
105-
106-
.. code-block:: javascript
107-
108-
ObjectId("507f191e810c19729de860ea").str
109-
110-
This operation will return the following hexadecimal string:
100+
The method returns:
111101

112102
.. code-block:: none
113103

@@ -116,23 +106,39 @@ This operation will return the following hexadecimal string:
116106
Specify an Integer String
117107
~~~~~~~~~~~~~~~~~~~~~~~~~
118108

119-
Generate a new ObjectId using an integer.
109+
If you want to adjust the ObjectId timestamp, use an integer to generate
110+
a new ObjectId.
120111

121112
.. code-block:: javascript
122113

123114
newObjectId = ObjectId(32)
124115

125-
The ObjectId resembles:
116+
The ObjectId value resembles:
126117

127118
.. code-block:: javascript
128119

129120
ObjectId("00000020f51bb4362eee2a4d")
130121

131-
The first four bytes of the ObjectId are the number of seconds since
132-
the :wikipedia:`Unix epoch`. In this example ``32`` seconds,
133-
represented in hexadecimal as ``00000020``, are added. A five byte
134-
random element and a three byte counter make up the rest of the
135-
ObjectId.
122+
The example ObjectId consists of:
123+
124+
- A four byte time stamp, ``00000020``
125+
- A five byte random element, ``f51bb4362e``
126+
- A three byte counter, ``ee2a4d``
127+
128+
The first four bytes of the ObjectId are the number of seconds since the
129+
:wikipedia:`Unix epoch`. In this example, the ObjectId timestamp is
130+
``00000020`` which is ``32`` in hexadecimal.
131+
132+
Specify a Hexadecimal String
133+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134+
135+
If you want to use a hexadecimal string to specify an ObjectId, pass a
136+
unique, 24 character hexadecimal value when you call
137+
:method:`ObjectId()`:
138+
139+
.. code-block:: javascript
140+
141+
newObjectId = ObjectId("507f191e810c19729de860ea")
136142

137143
.. seealso::
138144

0 commit comments

Comments
 (0)