@@ -1415,8 +1415,47 @@ requiring the usage of the aggregation pipeline for both queries and updates,
14151415applications should avoid using dots in field names and starting field names
14161416with 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