Skip to content

Commit 3d1f144

Browse files
Schuyler JagerEmily Giurleop
authored
MONGOID-4902 Include shard key when reloading documents for improved query performance with Atlas Global Write clusters (#4791)
* Use atomic selector for document reload and update docs for Atlas Global Cluster * Sharding docs formatting tweak * add a test to check that reload uses shard key on sharded cluster * add a test for embedded document * Cleaned up some unnecessary test code * added a note to the change log * added info to change logs and documentation * move changelog info to new 7.2 section * tweak documentation Co-authored-by: Emily Giurleo <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 3aa35d3 commit 3d1f144

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

source/tutorials/mongoid-upgrade.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,55 @@ please consult GitHub releases for detailed release notes and JIRA for
1818
the complete list of issues fixed in each release, including bug fixes.
1919

2020

21+
Upgrading to Mongoid 7.2
22+
========================
23+
24+
The following sections describe major changes in Mongoid 7.2.
25+
26+
Shard Key Used For Reloading
27+
----------------------------
28+
29+
Minor change: When sharding is used, Mongoid 7.2 expects the shard key declared
30+
in models to match the shard key in the database for the respective collections.
31+
In Mongoid 7.2 model reloading (either explicit via the ``reload`` method
32+
or implicit as part of persistence operations) uses the shard key, if one is
33+
defined, in the ``find`` command in addition to the ``id`` field value.
34+
This improves the performance of document reloading, and consequently some
35+
persistence operations, in sharded clusters, especially those with
36+
`geographically distributed shards
37+
<https://docs.atlas.mongodb.com/global-clusters/>`_.
38+
39+
Consider a class ``Band`` whose documents are sharded by the ``name`` key.
40+
41+
.. code-block:: ruby
42+
43+
class Band
44+
include Mongoid::Document
45+
field :name, type: String
46+
47+
shard_key :name
48+
end
49+
50+
Example Mongoid 7.2 behavior:
51+
52+
.. code-block:: ruby
53+
54+
band = Band.create(name: "Destiny's Child")
55+
band.reload
56+
# Command logs: { "find"=>"bands", "filter"=>{ "_id"=>BSON::ObjectId('...') "name"=>"Destiny's Child" } }
57+
58+
Example Mongoid 7.1 behavior:
59+
60+
.. code-block:: ruby
61+
62+
band = Band.create(name: "Destiny's Child")
63+
band.reload
64+
# Command logs: { "find"=>"bands", "filter"=>{"_id"=>BSON::ObjectId('...') } }
65+
66+
Mongoid provides :ref:`sharding management Rake tasks <sharding-management>`
67+
to shard collections according to shard keys declared in models.
68+
69+
2170
Upgrading to Mongoid 7.1
2271
========================
2372

@@ -216,6 +265,7 @@ example, the following two approaches result in the same generated selector:
216265
# class: Band
217266
# embedded: false>
218267

268+
219269
Ruby Version Support
220270
--------------------
221271

source/tutorials/sharding.txt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ Shard keys can be declared on models using the ``shard_key`` macro:
2525

2626
class Person
2727
include Mongoid::Document
28-
28+
2929
field :ssn
3030

3131
shard_key ssn: 1
32-
32+
3333
# The collection must also have an index that starts with the shard key.
3434
index ssn: 1
3535
end
@@ -48,11 +48,11 @@ collection sharding options:
4848
.. code-block:: ruby
4949

5050
shard_key ssn: 1
51-
51+
5252
shard_key ssn: 1, country: 1
53-
53+
5454
shard_key ssn: :hashed
55-
55+
5656
shard_key {ssn: :hashed}, unique: true
5757

5858
The alternative is the shorthand syntax, in which only the keys are given.
@@ -62,7 +62,7 @@ be specified:
6262
.. code-block:: ruby
6363

6464
shard_key :ssn
65-
65+
6666
shard_key :ssn, :country
6767

6868
``shard_key`` macro can take the name of a ``belongs_to`` association in
@@ -71,16 +71,26 @@ configured in the association as the field name:
7171

7272
class Person
7373
include Mongoid::Document
74-
74+
7575
belongs_to :country
7676

7777
# Shards by country_id.
7878
shard_key country: 1
79-
79+
8080
# The collection must also have an index that starts with the shard key.
8181
index country: 1
8282
end
8383

84+
.. note::
85+
86+
If a model declares a shard key, Mongoid expects the respective collection
87+
to be sharded with the specified shard key. When reloading models, Mongoid
88+
will provide the shard key in addition to the ``id`` field value to the
89+
``find`` command to improve query performance, especially on `geographically
90+
distributed sharded clusters <https://docs.atlas.mongodb.com/global-clusters/>`_.
91+
If the collection is not sharded with the specified shard key, queries
92+
may produce incorrect results.
93+
8494

8595
.. _sharding-management:
8696

0 commit comments

Comments
 (0)