|
| 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