-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
literal.txt
126 lines (83 loc) · 3.14 KB
/
literal.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
======================
$literal (aggregation)
======================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. expression:: $literal
Returns a value without parsing. Use for values that the aggregation
pipeline may interpret as an expression.
The :expression:`$literal` expression has the following syntax:
.. code-block:: none
{ $literal: <value> }
Behavior
--------
If the ``<value>`` is an :ref:`expression <aggregation-expressions>`,
:expression:`$literal` does not evaluate the expression but instead
returns the unparsed expression.
.. list-table::
:header-rows: 1
:widths: 40, 20, 40
* - Example
-
- Result
* - ``{ $literal: { $add: [ 2, 3 ] } }``
-
- ``{ "$add" : [ 2, 3 ] }``
* - ``{ $literal: { $literal: 1 } }``
-
- ``{ "$literal" : 1 }``
Examples
--------
Treat ``$`` as a Literal
~~~~~~~~~~~~~~~~~~~~~~~~
In :ref:`expression <aggregation-expressions>`,
the dollar sign ``$`` evaluates to a field path; i.e. provides access
to the field. For example, the :operator:`$eq` expression ``$eq: [
"$price", "$1" ]`` performs an equality check between the value in the
field named ``price`` and the value in the field named ``1`` in the
document.
The following example uses a :expression:`$literal` expression to treat
a string that contains a dollar sign ``"$1"`` as a constant value.
A collection ``records`` has the following documents:
.. code-block:: javascript
{ "_id" : 1, "item" : "abc123", price: "$2.50" }
{ "_id" : 2, "item" : "xyz123", price: "1" }
{ "_id" : 3, "item" : "ijk123", price: "$1" }
.. code-block:: javascript
db.records.aggregate( [
{ $project: { costsOneDollar: { $eq: [ "$price", { $literal: "$1" } ] } } }
] )
This operation projects a field named ``costsOneDollar`` that holds a
boolean value, indicating whether the value of the ``price`` field is
equal to the string ``"$1"``:
.. code-block:: javascript
{ "_id" : 1, "costsOneDollar" : false }
{ "_id" : 2, "costsOneDollar" : false }
{ "_id" : 3, "costsOneDollar" : true }
Project a New Field with Value ``1``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The :pipeline:`$project` stage uses the expression ``<field>: 1`` to
include the ``<field>`` in the output. The following example uses the
:expression:`$literal` to return a new field set to the value of ``1``.
A collection ``bids`` has the following documents:
.. code-block:: javascript
{ "_id" : 1, "item" : "abc123", condition: "new" }
{ "_id" : 2, "item" : "xyz123", condition: "new" }
The following aggregation evaluates the expression ``item: 1`` to mean
return the existing field ``item`` in the output, but uses the
:expression:`{ $literal: 1 } <$literal>` expression to return a new
field ``startAt`` set to the value ``1``:
.. code-block:: javascript
db.bids.aggregate( [
{ $project: { item: 1, startAt: { $literal: 1 } } }
] )
The operation results in the following documents:
.. code-block:: javascript
{ "_id" : 1, "item" : "abc123", "startAt" : 1 }
{ "_id" : 2, "item" : "xyz123", "startAt" : 1 }