@@ -47,119 +47,125 @@ Behavior
47
47
Examples
48
48
--------
49
49
50
- Bitwise AND
51
- ~~~~~~~~~~~
52
-
53
- Consider the following document inserted into the collection
54
- ``switches``:
50
+ The following examples use the ``switches`` collection:
55
51
56
52
.. code-block:: javascript
57
53
58
- { _id: 1, expdata: NumberInt(13) }
54
+ db.switches.insertMany( [
55
+ { _id: 1, expdata: Int32(13) },
56
+ { _id: 2, expdata: Int32(3) },
57
+ { _id: 3, expdata: Int32(1) }
58
+ ] )
59
+
60
+ Bitwise AND
61
+ ~~~~~~~~~~~
59
62
60
- The following :method:`~db.collection.update()` operation updates the
61
- ``expdata`` field to the result of a bitwise ``and`` operation between
62
- the current value ``NumberInt(13)`` (i.e. ``1101``) and
63
- ``NumberInt(10)`` (i.e. ``1010``):
63
+ Use a bitwise ``and`` in the :method:`~db.collection.updateOne()`
64
+ operation to update ``expdata``.
64
65
65
66
.. code-block:: javascript
66
67
67
- db.switches.update (
68
+ db.switches.updateOne (
68
69
{ _id: 1 },
69
- { $bit: { expdata: { and: NumberInt(10 ) } } }
70
+ { $bit: { expdata: { and: Int32( 10 ) } } }
70
71
)
71
72
72
- The bitwise ``and`` operation results in the integer 8 (i.e. ``1000``) :
73
+ The bitwise ``and`` operation:
73
74
74
- .. code-block:: none
75
+ - gets the bitwise value of ``expdata``
76
+ - uses ``and`` to apply the bitwise value of Int32(10)
77
+ - updates ``expdata`` with the result, 1000
75
78
76
- 1101
77
- 1010
79
+ .. code-block:: javascript
80
+ :copyable: false
81
+
82
+ 1101 // expdata
83
+ 1010 // Int32(10)
78
84
----
79
85
1000
80
86
81
- And the updated document has the following value for ``expdata``:
87
+ Binary 1000 is equivalent to Int32(8). The
88
+ ``db.switches.find( { _id: 1 } )`` command returns the following
89
+ document:
82
90
83
91
.. code-block:: javascript
84
92
85
93
{ "_id" : 1, "expdata" : 8 }
86
94
87
- :binary:`~bin.mongosh` displays ``NumberInt(8)`` as ``8``.
88
95
89
96
Bitwise OR
90
97
~~~~~~~~~~
91
98
92
- Consider the following document inserted into the collection
93
- ``switches``:
94
-
95
- .. code-block:: javascript
96
-
97
- { _id: 2, expdata: NumberLong(3) }
98
-
99
- The following :method:`~db.collection.update()` operation updates the
100
- ``expdata`` field to the result of a bitwise ``or`` operation between
101
- the current value ``NumberLong(3)`` (i.e. ``0011``) and
102
- ``NumberInt(5)`` (i.e. ``0101``):
99
+ Use a bitwise ``or`` in the :method:`~db.collection.updateOne()`
100
+ operation to update ``expdata``.
103
101
104
102
.. code-block:: javascript
105
103
106
- db.switches.update (
104
+ db.switches.updateOne (
107
105
{ _id: 2 },
108
- { $bit: { expdata: { or: NumberInt(5 ) } } }
106
+ { $bit: { expdata: { or: Int32( 5 ) } } }
109
107
)
110
108
111
- The bitwise ``or`` operation results in the integer 7 (i.e. ``0111``):
109
+ The bitwise ``or`` operation:
110
+
111
+ - gets the bitwise value of ``expdata``
112
+ - uses ``or`` to apply the bitwise value of Int32(5)
113
+ - updates ``expdata`` with the result, 0111
112
114
113
- .. code-block:: none
115
+ .. code-block:: javascript
116
+ :copyable: false
114
117
115
- 0011
116
- 0101
118
+ 0111 // expdata
119
+ 0101 // Int32(5)
117
120
----
118
121
0111
119
122
120
- And the updated document has the following value for ``expdata``:
123
+ Binary 0111 is equivalent to Int32(7). The
124
+ ``db.switches.find( { _id: 2 } )`` command returns the following
125
+ document:
121
126
122
127
.. code-block:: javascript
123
128
124
- { "_id" : 2, "expdata" : NumberLong(7) }
129
+ { "_id" : 2, "expdata" : 7 }
130
+
125
131
126
132
Bitwise XOR
127
133
~~~~~~~~~~~
128
134
129
- Consider the following document in the collection ``switches``:
135
+ Use a bitwise ``xor`` in the :method:`~db.collection.updateOne()`
136
+ operation to update ``expdata``.
130
137
131
138
.. code-block:: javascript
132
139
133
- { _id: 3, expdata: NumberLong(1) }
134
-
135
- The following :method:`~db.collection.update()` operation updates the
136
- ``expdata`` field to the result of a bitwise ``xor`` operation between
137
- the current value ``NumberLong(1)`` (i.e. ``0001``) and
138
- ``NumberInt(5)`` (i.e. ``0101``):
139
-
140
- .. code-block:: javascript
141
-
142
- db.switches.update(
140
+ db.switches.updateOne(
143
141
{ _id: 3 },
144
- { $bit: { expdata: { xor: NumberInt(5 ) } } }
142
+ { $bit: { expdata: { xor: Int32( 5 ) } } }
145
143
)
146
144
147
- The bitwise ``xor `` operation results in the integer 4 :
145
+ The bitwise ``and `` operation:
148
146
149
- .. code-block:: none
147
+ - gets the bitwise value of ``expdata``
148
+ - uses ``and`` to apply the bitwise value of Int32(5)
149
+ - updates ``expdata`` with the result, 0100
150
150
151
- 0001
152
- 0101
151
+ .. code-block:: javascript
152
+ :copyable: false
153
+
154
+ 0001 // expdata
155
+ 0101 // Int32(5)
153
156
----
154
157
0100
155
158
156
- And the updated document has the following value for ``expdata``:
159
+ Binary 0100 is equivalent to ``Int32(4)``. The
160
+ ``db.switches.find( { _id: 3 } )`` command returns the following
161
+ document:
157
162
158
163
.. code-block:: javascript
159
164
160
- { "_id" : 3 , "expdata" : NumberLong(4) }
165
+ { "_id" : 1 , "expdata" : 4 }
161
166
162
167
.. seealso::
163
168
164
- - :method:`db.collection.update ()`
169
+ - :method:`db.collection.updateOne ()`
165
170
- :method:`db.collection.findAndModify()`
171
+
0 commit comments