Skip to content

Commit d2da25f

Browse files
authored
DOCSP-29681 bitOr Aggregation Operator (#3117)
* DOCSP-29681 bitOr Aggregation Operator * typo fix * JA feedback * rewording * wording consistency * DH feedback
1 parent 81336a2 commit d2da25f

File tree

6 files changed

+141
-4
lines changed

6 files changed

+141
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
If the operands include both integers and long values, MongoDB sign-extends the
2+
calculated integer result and returns a long value. Otherwise, if the operands
3+
include only integers or only longs, MongoDB returns results with the
4+
corresponding value type.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
If any arguments in the array are of a different data type such as a string,
2+
double, or decimal, MongoDB returns an error.

source/meta/aggregation-quick-reference.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ Index of Expression Operators
356356
- :group:`$avg`
357357
- :expression:`$binarySize`
358358
- :expression:`$bitAnd`
359+
- :expression:`$bitOr`
359360
- :expression:`$bsonSize`
360361
- :expression:`$ceil`
361362
- :expression:`$cmp`

source/reference/operator/aggregation.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ Alphabetical Listing of Expression Operators
274274

275275
.. versionadded:: 6.3
276276

277+
* - :expression:`$bitOr`
278+
279+
- Returns the result of a bitwise ``or`` operation on an array of ``int``
280+
or ``long`` values.
281+
282+
.. versionadded:: 6.3
283+
277284
* - :group:`$bottom`
278285

279286
- Returns the bottom element within a group according to the specified
@@ -1286,6 +1293,7 @@ Alphabetical Listing of Expression Operators
12861293
/reference/operator/aggregation/avg
12871294
/reference/operator/aggregation/binarySize
12881295
/reference/operator/aggregation/bitAnd
1296+
/reference/operator/aggregation/bitOr
12891297
/reference/operator/aggregation/bottom
12901298
/reference/operator/aggregation/bottomN
12911299
/reference/operator/aggregation/bsonSize

source/reference/operator/aggregation/bitAnd.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ The :expression:`$bitAnd` operator has the following syntax:
3232
Behavior
3333
--------
3434

35-
If the operands include both integers and long values, MongoDB sign-extends the
36-
calculated integer result and returns a long value. Otherwise, if the operands
37-
include only integers or only longs, MongoDB returns results with the
38-
corresponding value type.
35+
.. include:: /includes/fact-bitwise-integer-long-results.rst
3936

4037
.. include:: /includes/fact-mongosh-integer-long-constructors.rst
4138

39+
.. include:: /includes/fact-bitwise-type-error.rst
40+
4241
If any of the operands equate to ``null``, the operation returns ``null``.
4342

4443
Examples
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
====================
2+
$bitOr (aggregation)
3+
====================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Definition
14+
----------
15+
16+
.. versionadded:: 6.3
17+
18+
.. expression:: $bitOr
19+
20+
Returns the result of a bitwise ``or`` operation on an array of ``int`` and
21+
``long`` values.
22+
23+
Syntax
24+
------
25+
26+
The ``$bitOr`` operator has the following syntax:
27+
28+
.. code-block:: javascript
29+
30+
{ $bitOr: { [ <expression1>, <expression2>, ... ] }
31+
32+
Behavior
33+
--------
34+
35+
.. include:: /includes/fact-bitwise-integer-long-results.rst
36+
37+
.. include:: /includes/fact-mongosh-integer-long-constructors.rst
38+
39+
.. include:: /includes/fact-bitwise-type-error.rst
40+
41+
If the argument is an empty array, the operation returns ``NumberInt(0)``.
42+
43+
If any of the arguments in the array equate to ``null``, the operation returns
44+
``null``.
45+
46+
Examples
47+
--------
48+
49+
The examples on this page use the ``switches`` collection, which contains the
50+
following documents:
51+
52+
.. code-block:: javascript
53+
54+
db.switches.insertMany( [
55+
{ _id: 0, a: NumberInt(0), b: NumberInt(127) },
56+
{ _id: 1, a: NumberInt(2), b: NumberInt(3) },
57+
{ _id: 2, a: NumberInt(3), b: NumberInt(5) }
58+
] )
59+
60+
Bitwise ``OR`` with Two Integers
61+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62+
63+
The following aggregation uses the ``$bitOr`` operator in the
64+
:pipeline:`$project` stage:
65+
66+
.. code-block:: javascript
67+
68+
db.switches.aggregate( [
69+
{
70+
$project: {
71+
result: {
72+
$bitOr: [ "$a", "$b" ]
73+
}
74+
}
75+
}
76+
])
77+
78+
The operation returns the following results:
79+
80+
.. code-block:: javascript
81+
:copyable: false
82+
83+
[
84+
{ _id: 0, result: 127 },
85+
{ _id: 1, result: 3 },
86+
{ _id: 2, result: 7 }
87+
]
88+
89+
Bitwise ``OR`` with a Long and Integer
90+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91+
92+
The following aggregation uses the ``$bitOr`` operator in the
93+
:pipeline:`$project` stage:
94+
95+
.. code-block:: javascript
96+
97+
db.switches.aggregate( [
98+
{
99+
$project: {
100+
result: {
101+
$bitOr: [ "$a", NumberLong("63") ]
102+
}
103+
}
104+
}
105+
])
106+
107+
The operation returns the following results:
108+
109+
.. code-block:: javascript
110+
:copyable: false
111+
112+
[
113+
{ _id: 0, result: Long("0") },
114+
{ _id: 1, result: Long("2") },
115+
{ _id: 2, result: Long("3") }
116+
]
117+
118+
Learn More
119+
----------
120+
121+
- :ref:`aggregation-pipeline-operators`
122+
123+
- :ref:`update-bit`

0 commit comments

Comments
 (0)