Skip to content

Commit f65ba65

Browse files
authored
MONGOID-5351 Implement Dots and Dollars in Mongoid (#5272)
* MONGOID-5300 fix dotted getter and add integration tests * MONGOID-5300 add docs * MONGOID-5351 add ProhibitedSetter error * MONGOID-5351 add ProhibitedSetter to docs * MONGOID-5351 import error and remove read_only stuff * MONGOID-5351 prohibit updates of dots and dollar fields * MONGOID-5351 correct language * MONGOID-5351 condition on driver 2.18 * Update docs/reference/fields.txt * MONGOID-5351 answer comments * MONGOID-5351 add api privates
1 parent cd29d7f commit f65ba65

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

source/reference/fields.txt

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,8 +1415,47 @@ requiring the usage of the aggregation pipeline for both queries and updates,
14151415
applications should avoid using dots in field names and starting field names
14161416
with the dollar sign if possible.
14171417

1418-
The Ruby driver `currently prohibits
1419-
<https://jira.mongodb.org/browse/RUBY-2528>`_ inserting documents whose
1420-
field names contain dots or begin with the dollar sign. However, if such
1421-
documents are inserted using other software, Mongoid and the Ruby driver
1422-
provide limited support for retrieving and operating on these documents.
1418+
Mongoid, starting in version 8, now allows users to access fields that begin with
1419+
dollar signs and that contain dots/periods. They can be accessed using the ``send``
1420+
method as follows:
1421+
1422+
.. code::
1423+
1424+
class User
1425+
include Mongoid::Document
1426+
field :"first.last", type: String
1427+
field :"$_amount", type: Integer
1428+
end
1429+
1430+
user = User.first
1431+
user.send(:"first.last")
1432+
# => Mike.Trout
1433+
user.send(:"$_amount")
1434+
# => 42650000
1435+
1436+
It is also possible to use ``read_attribute`` to access these fields:
1437+
1438+
.. code::
1439+
1440+
user.read_attribute("first.last")
1441+
# => Mike.Trout
1442+
1443+
Due to `server limitations <https://www.mongodb.com/docs/manual/core/dot-dollar-considerations/>`_,
1444+
updating and replacing fields containing dots and dollars requires using special
1445+
operators. For this reason, calling setters on these fields is prohibited and
1446+
will raise an error:
1447+
1448+
.. code::
1449+
1450+
class User
1451+
include Mongoid::Document
1452+
field :"first.last", type: String
1453+
field :"$_amount", type: Integer
1454+
end
1455+
1456+
user = User.new
1457+
user.send(:"first.last=", "Shohei.Ohtani")
1458+
# raises a InvalidDotDollarAssignment error
1459+
user.send(:"$_amount=", 8500000)
1460+
# raises a InvalidDotDollarAssignment error
1461+

0 commit comments

Comments
 (0)