|
1 | 1 | .. _field-names-periods-dollar-signs: |
| 2 | +.. _crud-concepts-dot-dollar-considerations: |
2 | 3 |
|
3 | | -========================================================= |
4 | | -Field Names with Periods (``.``) and Dollar Signs (``$``) |
5 | | -========================================================= |
| 4 | +========================================= |
| 5 | +Field Names with Periods and Dollar Signs |
| 6 | +========================================= |
6 | 7 |
|
7 | | -.. default-domain:: mongodb |
| 8 | +.. facet:: |
| 9 | + :name: genre |
| 10 | + :values: reference |
8 | 11 |
|
9 | 12 | .. contents:: On this page |
10 | 13 | :local: |
11 | 14 | :backlinks: none |
12 | 15 | :depth: 1 |
13 | 16 | :class: singlecol |
14 | 17 |
|
15 | | -.. _crud-concepts-dot-dollar-considerations: |
16 | | - |
17 | | -Overview |
18 | | --------- |
19 | | - |
20 | | -MongoDB 5.0 adds improved support for field names that are dollar |
21 | | -(``$``) prefixed or that contain periods (``.``). The validation rules |
22 | | -for storing data have been updated to make it easier to work with data |
23 | | -sources that use these characters. |
| 18 | +MongoDB supports field names that are dollar (``$``) prefixed or that |
| 19 | +contain periods (``.``). |
24 | 20 |
|
25 | 21 | In most cases data that has been stored using field names like these |
26 | 22 | is not directly accessible. You need to use helper methods like |
27 | 23 | :expression:`$getField`, :expression:`$setField`, and |
28 | | -:expression:`$literal` in queries that access those fields. |
| 24 | +:expression:`$literal` in queries that access those fields. |
29 | 25 |
|
30 | 26 | The field name validation rules are not the same for all types of |
31 | | -storage operations. This page summarizes how different insert and |
32 | | -update operations handle dollar (``$``) prefixed field names. |
33 | | - |
34 | | -Insert operations |
35 | | ------------------ |
36 | | - |
37 | | -Dollar (``$``) prefixed fields are permitted as top level and nested |
38 | | -field names for inserts. |
39 | | - |
40 | | -.. code-block:: javascript |
41 | | - :emphasize-lines: 3 |
42 | | - |
43 | | - db.sales.insertOne( { |
44 | | - "$price": 50.00, |
45 | | - "quantity": 30 |
46 | | - } ) |
47 | | - |
48 | | -Dollar (``$``) prefixed fields are permitted on inserts using otherwise |
49 | | -reserved words. Operator names like :update:`$inc` can be used as |
50 | | -field names as well as words like ``id``, ``db``, and ``ref``. |
51 | | - |
52 | | -.. code-block:: javascript |
53 | | - :emphasize-lines: 2, 4-6 |
54 | | - |
55 | | - db.books.insertOne( { |
56 | | - "$id": "h1961-01", |
57 | | - "location": { |
58 | | - "$db": "novels", |
59 | | - "$ref": "2007042768", |
60 | | - "$inc": true |
61 | | - } } ) |
62 | | - |
63 | | -An update which creates a new document during an :term:`upsert` is |
64 | | -treated as an ``insert`` rather than an ``update`` for field name |
65 | | -validation. :term:`Upserts <upsert>` can accept dollar (``$``) prefixed |
66 | | -fields. However, :term:`upserts <upsert>` are a special case and |
67 | | -similar update operations may cause an error if the ``match`` portion |
68 | | -of the update selects an existing document. |
69 | | - |
70 | | -This code sample has ``upsert: true`` so it will insert a new document |
71 | | -if the collection doesn't already contain a document that matches the |
72 | | -query term, ``{ "date": "2021-07-07" }``. If this sample code matches |
73 | | -an existing document, the update will fail since ``$hotel`` is dollar |
74 | | -(``$``) prefixed. |
75 | | - |
76 | | -.. code-block:: javascript |
77 | | - :emphasize-lines: 5 |
78 | | - |
79 | | - db.expenses.updateOne( |
80 | | - { "date": "2021-07-07" }, |
81 | | - { $set: { |
82 | | - "phone": 25.17, |
83 | | - "$hotel": 320.10 |
84 | | - } }, |
85 | | - { upsert: true } |
86 | | - ) |
87 | | - |
88 | | -Document Replacing Updates |
89 | | --------------------------- |
90 | | - |
91 | | -Update operators either replace existing fields with new documents |
92 | | -or else modify those fields. In cases where the update performs a |
93 | | -replacement, dollar (``$``) prefixed fields are not permitted as top |
94 | | -level field names. |
95 | | - |
96 | | -Consider a document like |
97 | | - |
98 | | -.. code-block:: javascript:: |
99 | | - |
100 | | - { |
101 | | - "_id": "E123", |
102 | | - "address": { |
103 | | - "$number": 123, |
104 | | - "$street": "Elm Road" |
105 | | - }, |
106 | | - "$rooms": { |
107 | | - "br": 2, |
108 | | - "bath": 1 |
109 | | - } |
110 | | - } |
111 | | - |
112 | | -You could use an update operator that replaces an existing document to |
113 | | -modify the ``address.$street`` field but you could not update the |
114 | | -``$rooms`` field that way. |
115 | | - |
116 | | -.. code-block:: |
117 | | - |
118 | | - db.housing.updateOne( |
119 | | - { "_id": "E123" }, |
120 | | - { $set: { "address.$street": "Elm Ave" } } |
121 | | - ) |
122 | | - |
123 | | -Use :expression:`$setField` as part of an aggregation pipeline to |
124 | | -:ref:`update top level <dotDollar-aggregate-update>` dollar (``$``) |
125 | | -prefixed fields like ``$rooms``. |
126 | | - |
127 | | -Document Modifying Updates |
128 | | --------------------------- |
129 | | - |
130 | | -When an update modifies, rather than replaces, existing document |
131 | | -fields, dollar (``$``) prefixed fields can be top level field names. |
132 | | -Subfields can be accessed directly, but you need a helper method to |
133 | | -access the top level fields. |
134 | | - |
135 | | -.. seealso:: |
136 | | - |
137 | | - :expression:`$getField`, :expression:`$setField`, |
138 | | - :expression:`$literal`, :pipeline:`$replaceWith` |
139 | | - |
140 | | -Consider a collection with documents like this inventory record: |
141 | | - |
142 | | -.. code-block:: |
143 | | - :copyable: false |
144 | | - |
145 | | - { |
146 | | - _id: ObjectId("610023ad7d58ecda39b8d161"), |
147 | | - "part": "AB305", |
148 | | - "$bin": 200, |
149 | | - "quantity": 100, |
150 | | - "pricing": { sale: true, "$discount": 60 } |
151 | | - } |
152 | | - |
153 | | -The ``pricing.$discount`` subfield can be queried directly. |
154 | | - |
155 | | -.. code-block:: |
156 | | - |
157 | | - db.inventory.findAndModify( { |
158 | | - query: { "part": { $eq: "AB305" } }, |
159 | | - update: { $inc: { "pricing.$discount": 10 } } |
160 | | - } ) |
161 | | - |
162 | | - |
163 | | -Use :expression:`$getField` and :expression:`$literal` to access the |
164 | | -value of the top level ``$bin`` field. |
165 | | - |
166 | | -.. code-block:: |
167 | | - :emphasize-lines: 3 |
168 | | - |
169 | | - db.inventory.findAndModify( { |
170 | | - query: { $expr: { |
171 | | - $eq: [ { $getField: { $literal: "$bin" } }, 200 ] |
172 | | - } }, |
173 | | - update: { $inc: { "quantity": 10 } } |
174 | | - } ) |
175 | | - |
176 | | -.. _dotDollar-aggregate-update: |
177 | | - |
178 | | -Updates Using Aggregation Pipelines |
179 | | ------------------------------------ |
180 | | - |
181 | | -Use :expression:`$setField`, :expression:`$getField`, and |
182 | | -:expression:`$literal` in the :pipeline:`$replaceWith` stage to modify |
183 | | -dollar (``$``) prefixed fields in an aggregation :term:`pipeline`. |
184 | | - |
185 | | -Consider a collection of school records like: |
186 | | - |
187 | | -.. code-block:: javascript |
188 | | - :copyable: false |
189 | | - |
190 | | - { |
191 | | - "_id": 100001, |
192 | | - "$term": "fall", |
193 | | - "registered": true, |
194 | | - "grade": 4 |
195 | | - } |
196 | | - |
197 | | -Create a new collection for the spring semester using a |
198 | | -:term:`pipeline` to update the dollar (``$``) prefixed ``$term`` field. |
199 | | - |
200 | | -.. code-block:: javascript |
201 | | - :emphasize-lines: 3-5 |
202 | | - |
203 | | - db.school.aggregate( [ |
204 | | - { $match: { "registered": true } }, |
205 | | - { $replaceWith: { |
206 | | - $setField: { |
207 | | - field: { $literal: "$term" }, |
208 | | - input: "$$ROOT", |
209 | | - value: "spring" |
210 | | - } } }, |
211 | | - { $out: "spring2022" } |
212 | | - ] ) |
| 27 | +storage operations. |
213 | 28 |
|
214 | | -General Restrictions |
215 | | --------------------- |
| 29 | +Get Started |
| 30 | +----------- |
216 | 31 |
|
217 | | -In addition to the storage validation rules above, there are some |
218 | | -general restrictions on using dollar (``$``) prefixed field names. |
219 | | -These fields cannot: |
| 32 | +For examples of how to handle field names that contain periods and |
| 33 | +dollar signs, see these pages: |
220 | 34 |
|
221 | | -- Be indexed |
222 | | -- Be used as part of a shard key |
223 | | -- Be validated using :query:`$jsonSchema` |
224 | | -- Be be modified with an escape sequence |
225 | | -- Be used with |
226 | | - :driver:`Field Level Encryption </security/client-side-field-level-encryption-guide>` |
227 | | -- Be used as a subfield in an ``_id`` document |
| 35 | +- :ref:`dollar-prefix-field-names` |
228 | 36 |
|
229 | | -.. include:: /includes/warning-possible-data-loss.rst |
| 37 | +- :ref:`period-field-names` |
230 | 38 |
|
231 | | -.. include:: /includes/warning-dot-dollar-import-export.rst |
| 39 | +.. toctree:: |
| 40 | + :titlesonly: |
232 | 41 |
|
| 42 | + /core/dot-dollar-considerations/dollar-prefix |
| 43 | + /core/dot-dollar-considerations/periods |
0 commit comments