Skip to content

Commit 5ee935a

Browse files
mungitoperritojeff-allen-mongo
authored andcommitted
DOCSP-17524-warning-for-dot-dollar-import-export
1 parent ddd5cef commit 5ee935a

10 files changed

+100
-63
lines changed

source/core/dot-dollar-considerations.txt

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
====================================
2-
Field Names with (``.``) and (``$``)
3-
====================================
1+
=========================================================
2+
Field Names with Periods (``.``) and Dollar Signs (``$``)
3+
=========================================================
44

55
.. default-domain:: mongodb
66

@@ -15,9 +15,9 @@ Field Names with (``.``) and (``$``)
1515
Overview
1616
--------
1717

18-
MongoDB 5.0 adds improved support for field names that are (``$``)
19-
prefixed or that contain (``.``) characters. The validation rules for
20-
storing data have been updated to make it easier to work with data
18+
MongoDB 5.0 adds improved support for field names that are dollar
19+
(``$``) prefixed or that contain periods (``.``). The validation rules
20+
for storing data have been updated to make it easier to work with data
2121
sources that use these characters.
2222

2323
In most cases data that has been stored using field names like these
@@ -27,13 +27,13 @@ is not directly accessible. You need to use helper methods like
2727

2828
The field name validation rules are not the same for all types of
2929
storage operations. This page summarizes how different insert and
30-
update operations handle (``$``) prefixed field names.
30+
update operations handle dollar (``$``) prefixed field names.
3131

3232
Insert operations
3333
-----------------
3434

35-
(``$``) prefixed fields are permitted as top level and nested field
36-
names for inserts.
35+
Dollar (``$``) prefixed fields are permitted as top level and nested
36+
field names for inserts.
3737

3838
.. code-block:: javascript
3939
:emphasize-lines: 3
@@ -43,7 +43,7 @@ names for inserts.
4343
"quantity": 30
4444
} )
4545

46-
(``$``) prefixed fields are permitted on inserts using otherwise
46+
Dollar (``$``) prefixed fields are permitted on inserts using otherwise
4747
reserved words. Operator names like :update:`$inc` can be used as
4848
field names as well as words like ``id``, ``db``, and ``ref``.
4949

@@ -60,16 +60,16 @@ field names as well as words like ``id``, ``db``, and ``ref``.
6060

6161
An update which creates a new document during an :term:`upsert` is
6262
treated as an ``insert`` rather than an ``update`` for field name
63-
validation. :term:`Upserts <upsert>` can accept (``$``) prefixed
63+
validation. :term:`Upserts <upsert>` can accept dollar (``$``) prefixed
6464
fields. However, :term:`upserts <upsert>` are a special case and
6565
similar update operations may cause an error if the ``match`` portion
6666
of the update selects an existing document.
6767

6868
This code sample has ``upsert: true`` so it will insert a new document
6969
if the collection doesn't already contain a document that matches the
7070
query term, ``{ "date": "2021-07-07" }``. If this sample code matches
71-
an existing document, the update will fail since ``$hotel`` is (``$``)
72-
prefixed.
71+
an existing document, the update will fail since ``$hotel`` is dollar
72+
(``$``) prefixed.
7373

7474
.. code-block:: javascript
7575
:emphasize-lines: 5
@@ -88,8 +88,8 @@ Document Replacing Updates
8888

8989
Update operators either replace existing fields with new documents
9090
or else modify those fields. In cases where the update performs a
91-
replacement, (``$``) prefixed fields are not permitted as top level
92-
field names.
91+
replacement, dollar (``$``) prefixed fields are not permitted as top
92+
level field names.
9393

9494
Consider a document like
9595

@@ -119,14 +119,14 @@ modify the ``address.$street`` field but you could not update the
119119
)
120120

121121
Use :expression:`$setField` as part of an aggregation pipeline to
122-
:ref:`update top level <dotDollar-aggregate-update>` (``$``) prefixed
123-
fields like ``$rooms``.
122+
:ref:`update top level <dotDollar-aggregate-update>` dollar (``$``)
123+
prefixed fields like ``$rooms``.
124124

125125
Document Modifying Updates
126126
--------------------------
127127

128128
When an update modifies, rather than replaces, existing document
129-
fields, (``$``) prefixed fields can be top level field names.
129+
fields, dollar (``$``) prefixed fields can be top level field names.
130130
Subfields can be accessed directly, but you need a helper method to
131131
access the top level fields.
132132

@@ -178,7 +178,7 @@ Updates Using Aggregation Pipelines
178178

179179
Use :expression:`$setField`, :expression:`$getField`, and
180180
:expression:`$literal` in the :pipeline:`$replaceWith` stage to modify
181-
(``$``) prefixed fields in an aggregation :term:`pipeline`.
181+
dollar (``$``) prefixed fields in an aggregation :term:`pipeline`.
182182

183183
Consider a collection of school records like:
184184

@@ -193,7 +193,7 @@ Consider a collection of school records like:
193193
}
194194

195195
Create a new collection for the spring semester using a
196-
:term:`pipeline` to update the (``$``) prefixed ``$term`` field.
196+
:term:`pipeline` to update the dollar (``$``) prefixed ``$term`` field.
197197

198198
.. code-block:: javascript
199199
:emphasize-lines: 3-5
@@ -213,8 +213,8 @@ General Restrictions
213213
--------------------
214214

215215
In addition to the storage validation rules above, there are some
216-
general restrictions on using (``$``) prefixed field names. These
217-
fields cannot:
216+
general restrictions on using dollar (``$``) prefixed field names.
217+
These fields cannot:
218218

219219
- Be indexed
220220
- Be used as part of a shard key
@@ -226,3 +226,5 @@ fields cannot:
226226

227227
.. include:: /includes/warning-possible-data-loss.rst
228228

229+
.. include:: /includes/warning-dot-dollar-import-export.rst
230+

source/includes/extracts-projection.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ content: |
8181
8282
db.inventory.find( {}, { "$instock.warehouse": 0, "$item": 0, "detail.$price": 1 } ) // Invalid starting in 4.4
8383
84-
MongoDB already has a :limit:`restriction <Restrictions on Field Names>`
85-
where top-level field names cannot start with the dollar sign
86-
(``$``).
87-
8884
In earlier version, MongoDB ignores the ``$``-prefixed field
8985
projections.
9086
---
@@ -103,10 +99,6 @@ content: |
10399
104100
db.inventory.find( { "instock.qty": { $gt: 25 } }, { "instock.$": { $slice: 1 } } ) // Invalid starting in 4.4
105101
106-
MongoDB already has a :limit:`restriction <Restrictions on Field
107-
Names>` where top-level field names cannot start with the dollar sign
108-
(``$``).
109-
110102
In previous versions, MongoDB returns the first element
111103
(``instock.$``) in the ``instock`` array that matches the query
112104
condition; i.e. the positional projection ``"instock.$"`` takes
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
The MongoDB Query Language is undefined over documents with duplicate
2+
field names. BSON builders may support creating a BSON document with
3+
duplicate field names. While the BSON builder may not throw an error,
4+
inserting these documents into MongoDB is not supported *even if* the
5+
insert succeeds. For example, inserting a BSON document with duplicate
6+
field names through a MongoDB driver may result in the driver silently
7+
dropping the duplicate values prior to insertion.
8+

source/includes/warning-document-duplicate-key-names.rst

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Starting in MongoDB 5.0, document field names can be dollar (``$``)
2+
prefixed and can contain periods (``.``). However,
3+
:binary:`~bin.mongoimport` and :binary:`~bin.mongoexport` may not work
4+
as expected in some situations with field names that make use of these
5+
characters.
6+
7+
:ref:`MongoDB Extended JSON v2 <extended-json-high-level-ref-v2>`
8+
cannot differentiate between type wrappers and fields that happen to
9+
have the same name as type wrappers. Do not use Extended JSON
10+
formats in contexts where the corresponding BSON representations
11+
might include dollar (``$``) prefixed keys. The
12+
:ref:`DBRef <dbref-explanation>` mechanism is an exception to this
13+
general rule.
14+
15+
There are also restrictions on using :binary:`~bin.mongoimport` and
16+
:binary:`~bin.mongoexport` with periods (``.``) in field names. Since
17+
CSV files use the period (``.``) to represent data hierarchies, a
18+
period (``.``) in a field name will be misinterpreted as a level of
19+
nesting.
20+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. warning:: Import and Export Concerns With Dollar Signs (``$``) and Periods (``.``)
2+
3+
.. include:: /includes/warning-dot-dollar-import-export-body.rst
4+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
There is a small chance of data loss when using dollar (``$``) prefixed
2+
field names or field names that contain periods (``.``) if these
3+
field names are used in conjunction with unacknowledged writes
4+
(:doc:`write concern </reference/write-concern>` ``w=0``) on servers
5+
that are older than MongoDB 5.0.
6+
7+
When running :doc:`insert </tutorial/insert-documents>`,
8+
:doc:`update </tutorial/insert-documents>`, and
9+
:doc:`findAndModify </reference/method/db.collection.findAndModify>`
10+
commands, drivers that are 5.0 compatible remove restrictions on
11+
using documents with field names that are dollar (``$``) prefixed or
12+
that contain periods (``.``). These field names generated a client-side
13+
error in earlier driver versions.
14+
15+
The restrictions are removed regardless of the server version the
16+
driver is connected to. If a 5.0 driver sends a document to an older
17+
server, the document will be rejected without sending an error.
18+
Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
1-
.. warning::
1+
.. warning:: Possible Data Loss With Dollar Signs (``$``) and Periods (``.``)
22

3-
There is a small chance of data loss when using (``$``) prefixed
4-
field names or field names with (``.``) characters when this type of
5-
field name is used in conjunction with unacknowledged writes
6-
(:doc:`write concern </reference/write-concern>` ``w=0``) on servers
7-
that are older than MongoDB 5.0.
8-
9-
When running :doc:`insert </tutorial/insert-documents>`,
10-
:doc:`update </tutorial/insert-documents>`, and
11-
:doc:`findAndModify </reference/method/db.collection.findAndModify>`
12-
commands, drivers that are 5.0 compatible remove restrictions on
13-
using documents with field names that are (``$``) prefixed or that
14-
contain (``.``) characters. These field names generated a
15-
client-side error in earlier driver versions.
16-
17-
The restrictions are removed regardless of the server version the
18-
driver is connected to. If a 5.0 driver sends a document to an older
19-
server, the document will be rejected without sending an error.
3+
.. include:: /includes/warning-possible-data-loss-body.rst
204

source/reference/limits.txt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,31 @@ Naming Restrictions
9393

9494
.. include:: /includes/fact-document-field-name-restrictions.rst
9595

96-
.. include:: /includes/warning-possible-data-loss.rst
96+
.. limit:: Restrictions on _id
9797

98-
.. include:: /includes/warning-document-duplicate-key-names.rst
98+
.. include:: /includes/fact-id-field-name-rules.rst
9999

100-
.. limit:: Restrictions on ``_id``
100+
Naming Warnings
101+
---------------
101102

102-
.. include:: /includes/fact-id-field-name-rules.rst
103+
.. warning::
104+
105+
Use caution, the issues discussed in this section could lead to data
106+
loss or corruption.
107+
108+
MongoDB does not support duplicate field names
109+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110+
.. include:: /includes/warning-document-duplicate-key-names-body.rst
111+
112+
Import and Export Concerns With Dollar Signs (``$``) and Periods (``.``)
113+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114+
115+
.. include:: /includes/warning-dot-dollar-import-export-body.rst
116+
117+
Possible Data Loss With Dollar Signs (``$``) and Periods (``.``)
118+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119+
120+
.. include:: /includes/warning-possible-data-loss-body.rst
103121

104122
.. _faq-dev-namespace:
105123

source/reference/mongodb-extended-json.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ MongoDB Extended JSON (v2)
1010
:depth: 1
1111
:class: singlecol
1212

13+
.. _extended-json-high-level-ref-v2:
14+
1315
.. important:: Disambiguation
1416

1517
The following page discusses MongoDB Extended JSON v2. For

0 commit comments

Comments
 (0)