Skip to content

Commit 4e3fb04

Browse files
committed
and and or
1 parent 900f203 commit 4e3fb04

File tree

1 file changed

+80
-18
lines changed

1 file changed

+80
-18
lines changed

source/fundamentals/linq.txt

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -588,52 +588,114 @@ in the Atlas manual. For more examples about running Atlas Vector Search queries
588588
{+driver-short+}, see :atlas:`Run Vector Search Queries </atlas-vector-search/vector-search-stage/>`
589589
in the Atlas manual and select :guilabel:`C#` from the language dropdown.
590590

591+
Bitwise Operators
592+
~~~~~~~~~~~~~~~~~
593+
594+
This version of the {+driver-short+} supports the following bitwise operators in
595+
the aggregation pipeline. You can use multiple bitwise operators within the same
596+
stage. All operands must be of type ``int`` or ``long``. ``$bitAnd``, ``$bitOr``,
597+
and ``$bitXor`` all take two or more operands. ``$bitNot`` can only take one
598+
operand.
599+
600+
The examples for this section use the following documents in a collection called
601+
``ingredients``:
602+
603+
.. code-block:: json
604+
605+
{ "_id": 1, "name": "watermelon", "price": 5, "count": 1 },
606+
{ "_id": 2, "name": "onions", "price": 2, "count": 4 },
607+
{ "_id": 3, "name": "eggs", "price": 5, "count": 12 },
608+
{ "_id": 4, "name": "potatoes", "price": 3, "count": 0 },
609+
{ "_id": 5, "name": "pasta", "price": 4, "count": 100 },
610+
{ "_id": 6, "name": "cheese", "price": 4 }
611+
612+
The following ``Ingredient`` class models the documents in the ``ingredients``
613+
collection:
614+
615+
.. code-block:: csharp
616+
617+
public class Ingredient {
618+
public int Id { get; set; }
619+
public string Name { get; set; }
620+
public int Price { get; set; }
621+
public int Count { get; set; }
622+
}
623+
624+
.. note::
625+
626+
For all bitwise operators, if the operand list contains a missing or undefined
627+
value, the entire expression evaluates to ``null``.
628+
629+
591630
$bitAnd
592-
~~~~~~~
631+
+++++++
593632

594633
The ``$bitAnd`` aggregation stage performs a bitwise AND operation on the given
595-
arguments. ``$bitAnd`` can take // describe arguments
634+
arguments. The following example shows how to generate a ``$bitAnd`` stage using
635+
LINQ:
596636

597637
.. code-block:: csharp
598638

599-
var queryable = collection.AsQueryable()
600-
.Select(x => x.P & x.Q
601-
);
639+
var query = queryableCollection.AsQueryable()
640+
.Where(i => i.Name == "eggs")
641+
.Select(x => x.Price & x.Count);
602642

603-
The result of the preceding example look like this:
643+
The preceding example finds the document where the ``Name`` field has the value
644+
``"eggs"``. It then performs a bitwise AND operation on the values of the
645+
``Price`` and ``Count`` fields in this document. The result contains one value:
646+
``4``.
647+
648+
The following example performs the same bitwise AND operation, but on all
649+
documents in the collection:
650+
651+
.. code-block:: csharp
604652

605-
// what should the parameters look like?
653+
var query = queryableCollection.AsQueryable()
654+
.Select(x => x.Price & x.Count);
606655

607-
// 2 arguments <-- the example should correspond to the pagewide example
608-
// 3 arguments
656+
The result of the preceding example contains the following values.
609657

610-
// restaurants collection
658+
.. code-block:: json
659+
660+
1
661+
0
662+
4
663+
0
664+
4
665+
null
611666

667+
Note that the ``null`` result comes from the document where the ``Name`` field
668+
has the value of ``"cheese"``. This document is missing a ``Count`` field, so
669+
the expression evaluates to ``null``.
612670

613671
$bitOr
614672
~~~~~~
615673

616674
The ``$bitOr`` aggregation stage performs a bitwise OR operation on the given
617-
arguments. ``$bitOr`` can take // describe arguments - same as AND and XOR
675+
arguments. The following example shows how to generate a ``$bitOr`` stage using
676+
LINQ:
618677

619678
.. code-block:: csharp
620679

621-
var queryable = collection.AsQueryable()
622-
.Select(x => x.P | x.Q
623-
);
680+
var query = queryableCollection.AsQueryable()
681+
.Where(i => i.Name == "eggs")
682+
.Select(x => x.Price | x.Count);
624683

625-
The result of the preceding example look like this:
684+
The preceding example finds the document where the ``Name`` field has the value
685+
``"eggs"``. It then performs a bitwise OR operation on the values of the
686+
``Price`` and ``Count`` fields in this document. The result contains one value:
687+
``13``.
626688

627689
$bitNot
628690
~~~~~~~
629691

630692
The ``$bitNot`` aggregation stage performs a bitwise NOT operation on the given
631-
argument. ``$bitOr`` only takes one argument.
693+
argument. ``$bitNot`` only takes one argument.
632694

633695
.. code-block:: csharp
634696

635697
var queryable = collection.AsQueryable()
636-
.Select(x => !x.P
698+
.Select(x => ~x.P
637699
);
638700

639701
The result of the preceding example looks like this:
@@ -645,7 +707,7 @@ The ``$bitXor`` aggregation stage performs a bitwise XOR operation on the given
645707
arguments. ``$bitXor`` can take // describe arguments - same as AND and XOR
646708

647709
.. code-block:: csharp
648-
710+
649711
var queryable = collection.AsQueryable()
650712
.Select(x => x.P ^ x.Q
651713
);

0 commit comments

Comments
 (0)