1
+ .. _server-objectid:
2
+
1
3
==========
2
4
ObjectId()
3
5
==========
@@ -28,10 +30,10 @@ Description
28
30
.. list-table::
29
31
:header-rows: 1
30
32
:widths: 20 80
31
-
33
+
32
34
* - Input Type
33
35
- Description
34
-
36
+
35
37
* - ``hexadecimal``
36
38
- Optional. A 24 character hexadecimal string value for the new
37
39
ObjectId.
@@ -41,28 +43,31 @@ Description
41
43
:wikipedia:`Unix epoch` to create the new timestamp.
42
44
43
45
44
- Methods and Attributes
45
- -----------------------
46
+ Methods
47
+ -------
46
48
47
- :method:`ObjectId()` has the following attribute and methods:
49
+ :method:`ObjectId()` has the following methods:
48
50
49
51
.. list-table::
50
52
:header-rows: 1
51
53
:widths: 30 70
52
54
53
- * - Attribute/Method
55
+ * - Methods
54
56
- Description
55
-
56
- * - ``str``
57
- - Returns the hexadecimal string representation of the object.
57
+
58
58
* - :method:`ObjectId.getTimestamp()`
59
59
- Returns the timestamp portion of the object as a Date.
60
+
60
61
* - :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
+
63
64
* - :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
66
71
67
72
Examples
68
73
--------
@@ -74,40 +79,25 @@ To generate a new ObjectId, use :method:`ObjectId()` with no argument:
74
79
75
80
.. code-block:: javascript
76
81
77
- x = ObjectId()
82
+ newObjectId = ObjectId()
78
83
79
- In this example, the value of ``x `` is:
84
+ In this example, the value of ``newObjectId `` is:
80
85
81
86
.. code-block:: javascript
82
87
83
88
ObjectId("507f1f77bcf86cd799439011")
84
89
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
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
94
92
95
- In this example, the value of ``y`` would be:
93
+ To return the ObjectId as a hexadecimal string, use the ``toString()``
94
+ method.
96
95
97
96
.. code-block:: javascript
98
97
99
- ObjectId("507f191e810c19729de860ea")
98
+ ObjectId("507f191e810c19729de860ea").toString()
100
99
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:
111
101
112
102
.. code-block:: none
113
103
@@ -116,23 +106,39 @@ This operation will return the following hexadecimal string:
116
106
Specify an Integer String
117
107
~~~~~~~~~~~~~~~~~~~~~~~~~
118
108
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.
120
111
121
112
.. code-block:: javascript
122
113
123
114
newObjectId = ObjectId(32)
124
115
125
- The ObjectId resembles:
116
+ The ObjectId value resembles:
126
117
127
118
.. code-block:: javascript
128
119
129
120
ObjectId("00000020f51bb4362eee2a4d")
130
121
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")
136
142
137
143
.. seealso::
138
144
0 commit comments