Skip to content

Commit 43361aa

Browse files
authored
Merge pull request #826 from steveren/DOCS-8717
DOCS-8717: document Decimal128 in Ruby driver
2 parents fedf2c8 + ed679da commit 43361aa

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

source/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ For tutorials on Mongoid, see :doc:`/mongoid`.
5656
.. toctree::
5757
:titlesonly:
5858

59+
whats-new
5960
installation
6061
quick-start
6162
ruby-driver-tutorials

source/tutorials/ruby-driver-crud-operations.txt

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,51 @@ On MongoDB 2.4, an exception is only raised if the insert fails and the
8585
])
8686
result.inserted_count # returns 2, because 2 documents were inserted.
8787

88+
.. _specify-decimal128:
89+
90+
Specify a ``Decimal128`` number
91+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92+
93+
.. versionadded:: 3.4
94+
95+
:manual:`Decimal128</tutorial/model-monetary-data>` is a
96+
:doc:`BSON datatype </bson-tutorials>`
97+
that employs 128-bit decimal-based floating-point values capable
98+
of emulating decimal rounding with exact precision. This
99+
functionality is intended for applications that handle
100+
:manual:`monetary data </tutorial/model-monetary-data>`,
101+
such as financial and tax computations.
102+
103+
The following example inserts a value of type ``Decimal128`` into
104+
the ``price`` field of a collection named ``inventory``:
105+
106+
.. code-block:: ruby
107+
108+
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
109+
110+
price = BSON::Decimal128.new("428.79")
111+
client[:inventory].insert_one({ "_id" => 1,
112+
"item" => "26 inch monitor",
113+
"price" => price })
114+
115+
The above operation produces the following document:
116+
117+
.. code-block:: javascript
118+
119+
{ "_id" : 1, "item" : "26 inch monitor", "price" : NumberDecimal("428.79") }
120+
121+
You can also create a ``Decimal128`` object from a Ruby ``BigDecimal``
122+
object, or with ``Decimal128.from_string()``.
123+
124+
.. code-block:: ruby
125+
126+
big_decimal = BigDecimal.new(428.79, 5)
127+
price = BSON::Decimal128.new(big_decimal)
128+
# => BSON::Decimal128('428.79')
129+
130+
price = BSON::Decimal128.from_string("428.79")
131+
# => BSON::Decimal128('428.79')
132+
88133
Reading
89134
-------
90135

@@ -110,7 +155,7 @@ query:
110155
.. _ruby-driver-query-options:
111156

112157
Query Options
113-
`````````````
158+
~~~~~~~~~~~~~
114159

115160
To add options to a query, chain the appropriate methods after the
116161
``find`` method. Note that the underlying object, the ``Mongo::Collection::View``,
@@ -182,7 +227,7 @@ when querying and their corresponding methods as examples.
182227
client[:artists].find.sort(:name => -1)
183228

184229
Additional Query Operations
185-
```````````````````````````
230+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
186231

187232
``count``
188233
Get the total number of documents an operation returns.
@@ -204,7 +249,7 @@ Additional Query Operations
204249
client[:artists].find.distinct(:name )
205250

206251
Tailable Cursors
207-
````````````````
252+
~~~~~~~~~~~~~~~~
208253

209254
For capped collections you may use a :manual:`tailable cursor
210255
</core/tailable-cursors/>` that remains open
@@ -233,7 +278,7 @@ following code example shows how a tailable cursor might be used:
233278
.. _ruby-driver-read-preference:
234279

235280
Read Preference
236-
````````````````
281+
~~~~~~~~~~~~~~~
237282

238283
Read preference determines the candidate :manual:`replica set</replication/>`
239284
members to which a query or command can be sent. They consist of a **mode** specified as
@@ -270,15 +315,15 @@ option when a command is run on a database.
270315
tag_sets: [ { 'dc' => 'nyc' } ] } )
271316

272317
Mode
273-
````
318+
~~~~
274319

275320
There are five possible read preference modes. They are ``:primary``,
276321
``:secondary``, ``:primary_preferred``, ``:secondary_preferred``,
277322
``:nearest``. Please see the :manual:`read preference documentation in the MongoDB Manual
278323
</core/read-preference/>` for an explanation of the modes and tag sets.
279324

280325
Tag sets
281-
````````
326+
~~~~~~~~
282327

283328
The ``tag_sets`` parameter is an ordered list of tag sets used to
284329
restrict the eligibility of servers for selection, such as for data

source/whats-new.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
==========
2+
What's New
3+
==========
4+
5+
.. default-domain:: mongodb
6+
7+
What's New in the 2.4 Driver
8+
----------------------------
9+
10+
- :doc:`Decimal128</tutorials/ruby-driver-crud-operations>`,
11+
a new :doc:`BSON datatype </bson-tutorials>` which employs 128-bit
12+
decimal-based floating-point values capable of emulating decimal
13+
rounding with exact precision.
14+
15+
.. COMMENT need internal link to #specify-decimal128 in the CRUD page
16+
.. COMMENT Collation and other items to come here

0 commit comments

Comments
 (0)