Skip to content

Commit 54c279c

Browse files
p-mongop
andauthored
MONGOID-4852 add release notes for 7.0 and document missing features (#4747)
* read-only attribute behavior * dependent behaviors * $unwind in aggregation pipeline dsl * order alphabetically * document all Mongoid options * reference the configuration options from everywhere they are applicable * finish 7.0 changes * add patch releases for 7.0 Co-authored-by: Oleg Pudeyev <oleg@bsdpower.com>
1 parent 2f2f9eb commit 54c279c

File tree

7 files changed

+251
-35
lines changed

7 files changed

+251
-35
lines changed

source/tutorials/mongoid-configuration.txt

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,37 @@ can be configured.
229229

230230
# Configure Mongoid specific options. (optional)
231231
options:
232+
# Application name that is printed to the mongodb logs upon establishing
233+
# a connection in server versions >= 3.4. Note that the name cannot
234+
# exceed 128 bytes. It is also used as the database name if the
235+
# database name is not explicitly defined. (default: nil)
236+
app_name: MyApplicationName
237+
238+
# Create indexes in background by default. (default: false)
239+
background_indexing: false
240+
241+
# Mark belongs_to associations as required by default, so that saving a
242+
# model with a missing belongs_to association will trigger a validation
243+
# error. (default: true)
244+
belongs_to_required_by_default: true
245+
246+
# Raise an exception when a field is redefined. (default: false)
247+
duplicate_fields_exception: false
248+
232249
# Include the root model name in json serialization. (default: false)
233250
include_root_in_json: false
234251

235252
# Include the _type field in serialization. (default: false)
236253
include_type_for_serialization: false
237254

255+
# Whether to join nested persistence contexts for atomic operations
256+
# to parent contexts by default. (default: false)
257+
join_contexts: false
258+
259+
# Set the Mongoid and Ruby driver log levels when Mongoid is not using
260+
# Ruby on Rails logger instance. (default: :info)
261+
log_level: :info
262+
238263
# Preload all models in development, needed when models use
239264
# inheritance. (default: false)
240265
preload_models: false
@@ -257,14 +282,6 @@ can be configured.
257282
# (default: false)
258283
use_utc: false
259284

260-
# Set the Mongoid and Ruby driver log levels when Mongoid is not using
261-
# Ruby on Rails logger instance. (default: :info)
262-
log_level: :info
263-
264-
# Whether to join nested persistence contexts for atomic operations
265-
# to parent contexts by default. (default: false)
266-
join_contexts: false
267-
268285
The Ruby driver options may be found in
269286
`the driver documentation <https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-create-client/>`_.
270287

source/tutorials/mongoid-documents.txt

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,41 @@ in object is a ``Point`` first, in case we also want to be able to pass in ordin
620620
Reserved Names
621621
**************
622622

623-
If you define a field on your document that conflicts with a reserved method name in Mongoid,
624-
the configuration will raise an error. For a list of these you may look at
625-
``Mongoid.destructive_fields``.
623+
Attempting to define a field on a document that conflicts with a reserved
624+
method name in Mongoid will raise an error. The list of reserved names can
625+
be obtained by invoking the ``Mongoid.destructive_fields`` method.
626+
627+
Field Redefinition
628+
******************
629+
630+
By default Mongoid allows redefining fields on a model. To raise an error
631+
when a field is redefined, set the ``duplicate_fields_exception``
632+
:ref:`configuration option <configuration-options>` to ``true``.
633+
634+
With the option set to true, the following example will raise an error:
635+
636+
.. code-block:: ruby
637+
638+
class Person
639+
include Mongoid::Document
640+
641+
field :name
642+
643+
field :name, type: String
644+
end
645+
646+
To define the field anyway, use the ``overwrite: true`` option:
647+
648+
.. code-block:: ruby
649+
650+
class Person
651+
include Mongoid::Document
652+
653+
field :name
654+
655+
field :name, type: String, overwrite: true
656+
end
657+
626658

627659
Custom Ids
628660
**********
@@ -1005,11 +1037,14 @@ calling ``Model#previous_changes``.
10051037
# View the previous changes.
10061038
person.previous_changes # { "name" => [ "Alan Parsons", "Alan Garner" ] }
10071039

1008-
Readonly Attributes
1040+
.. _read-only:
1041+
1042+
Read-Only Attributes
10091043
-------------------
10101044

1011-
You can tell Mongoid that certain attributes are readonly. This will allow documents to be
1012-
created with these attributes, but changes to them will be filtered out.
1045+
You can tell Mongoid that certain attributes are read-only. This will allow
1046+
documents to be created with these attributes, but changes to them will be
1047+
ignored when using mass update methods such as ``update_attributes``:
10131048

10141049
.. code-block:: ruby
10151050

@@ -1024,8 +1059,8 @@ created with these attributes, but changes to them will be filtered out.
10241059
band = Band.create(name: "Placebo")
10251060
band.update_attributes(name: "Tool") # Filters out the name change.
10261061

1027-
If you explicitly try to update or remove the attribute by itself, then a ``ReadonlyAttribute``
1028-
error will be raised.
1062+
If you explicitly try to update or remove a read-only attribute by itself,
1063+
a ``ReadonlyAttribute`` exception will be raised:
10291064

10301065
.. code-block:: ruby
10311066

source/tutorials/mongoid-indexes.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ the collection is large and index creation may take a long time:
7272
index({ ssn: 1 }, { unique: true, background: true })
7373
end
7474

75+
If the ``background_indexing`` :ref:`configuration option
76+
<configuration-options>` is true, the indexes are created in the background
77+
unless the ``background: false`` option is provided.
78+
7579
*Deprecated:* When using MongoDB 2.6, you can drop the duplicate entries
7680
for unique indexes that are defined for a column that might
7781
already have duplicate values by specifying the ``drop_dups`` option:

source/tutorials/mongoid-persistence.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,12 @@ changes performed by each block as soon as the block ends:
474474
end
475475

476476
This behavior can be changed by specifying the ``join_context: true`` option
477-
to ``#atomically``. When using this option, nested ``#atomically`` blocks
478-
are joined with the outer blocks, and only the outermost block (or the
479-
first block where ``join_contexts`` is false) actually writes changes to
480-
the cluster. For example:
477+
to ``#atomically``, or globally by setting the ``join_contexts``
478+
:ref:`configuration option <configuration-options>` to ``true``. When
479+
context joining is enabled, nested ``#atomically`` blocks are joined with
480+
the outer blocks, and only the outermost block (or the first block where
481+
``join_contexts`` is false) actually writes changes to the cluster.
482+
For example:
481483

482484
.. code-block:: ruby
483485

source/tutorials/mongoid-queries.txt

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,11 @@ Mongoid also has some helpful methods on criteria.
775775

776776
* - ``Criteria#find``
777777

778-
*Find a document or multiple documents by their ids. Will raise
779-
an error by default if any of the ids do not match.*
778+
*Find a document or multiple documents by their ids. If the*
779+
``raise_not_found_error`` *configuration option is set to* ``true``
780+
*which is the default and any of the ids are not found, raise an error.
781+
If the* ``raise_not_found_error`` *configuration option is set to*
782+
``false``, return an array of documents that have the specified ids.*
780783

781784
-
782785
.. code-block:: ruby
@@ -792,9 +795,9 @@ Mongoid also has some helpful methods on criteria.
792795

793796
* - ``Criteria#find_by``
794797

795-
*Find a document by the provided attributes, and if not found
796-
raise an error or return nil depending on the
797-
* ``raise_not_found_error`` *configuration option.*
798+
*Find a document by the provided attributes. If not found,
799+
raise an error or return nil depending on the value of the*
800+
``raise_not_found_error`` *configuration option.*
798801

799802
-
800803
.. code-block:: ruby
@@ -1186,7 +1189,6 @@ by a provided name. Just like normal criteria, they are lazy and chainable.
11861189

11871190
Band.english.rock # Get the English rock bands.
11881191

1189-
11901192
Named scopes can take procs and blocks for accepting parameters or
11911193
extending functionality.
11921194

@@ -1213,6 +1215,25 @@ extending functionality.
12131215
Band.named("Depeche Mode") # Find Depeche Mode.
12141216
Band.active.deutsch # Find active German bands.
12151217

1218+
By default, Mongoid allows defining a scope that would shadow an existing
1219+
class method, as the following example shows:
1220+
1221+
.. code-block:: ruby
1222+
1223+
class Product
1224+
include Mongoid::Document
1225+
1226+
def self.fresh
1227+
true
1228+
end
1229+
1230+
scope :fresh, ->{ where(fresh: true) }
1231+
end
1232+
1233+
To have Mongoid raise an error when a scope would overwrite an existing class
1234+
method, set the ``scope_overwrite_exception`` :ref:`configuration option
1235+
<configuration-options>` to ``true``.
1236+
12161237
Default Scopes
12171238
**************
12181239

source/tutorials/mongoid-relations.txt

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ Belongs To
132132
A ``belongs_to`` macro is used when a document is the child in a ``has_one``
133133
or ``has_many`` association. By default, in order for a document to
134134
be saved, each of its ``belongs_to`` associations must be provided a value.
135-
To override this requirement, you can use the option ``optional: false``
136-
on the ``belong_to`` association.
135+
To override this requirement for a particular association, use the option
136+
``optional: false`` on the ``belong_to`` association. To override this
137+
requirement globally, set the ``belongs_to_required_by_default``
138+
:ref:`configuration option <configuration-options>` to ``false``.
137139

138140
Defining
139141
~~~~~~~~
@@ -726,6 +728,8 @@ to the association.
726728

727729
band.save # Fires all save callbacks on the band, albums, and label.
728730

731+
.. _dependent-behavior:
732+
729733
Dependent Behavior
730734
******************
731735

@@ -1149,3 +1153,115 @@ the ``_id`` field which is then used to load full models. An alternative is
11491153
to not perform such a projection and work with raw fields, which would eliminate
11501154
having to send the list of document ids to Mongoid in the second query
11511155
(which could be large).
1156+
1157+
.. _aggregation-pipeline-builder-dsl:
1158+
1159+
Aggregation Pipeline Builder DSL
1160+
********************************
1161+
1162+
Mongoid provides limited support for constructing the aggregation pipeline
1163+
itself using a high-level DSL. The following aggregation pipeline operators
1164+
are supported:
1165+
1166+
- `$group <https://docs.mongodb.com/manual/reference/operator/aggregation/group/>`_
1167+
- `$project <https://docs.mongodb.com/manual/reference/operator/aggregation/project/>`_
1168+
- `$unwind <https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/>`_
1169+
1170+
To construct a pipeline, call the corresponding aggregation pipeine methods
1171+
on a ``Criteria`` instance. Aggregation pipeline operations are added to the
1172+
``pipeline`` attribute of the ``Criteria`` instance. To execute the pipeline,
1173+
pass the ``pipeline`` attribute value to ``Collection#aggragegate`` method.
1174+
1175+
For example, given the following models:
1176+
1177+
.. code-block:: ruby
1178+
1179+
class Tour
1180+
include Mongoid::Document
1181+
1182+
embeds_many :participants
1183+
1184+
field :name, type: String
1185+
field :states, type: Array
1186+
end
1187+
1188+
class Participant
1189+
include Mongoid::Document
1190+
1191+
embedded_in :tour
1192+
1193+
field :name, type: String
1194+
end
1195+
1196+
We could find out which states a participant visited:
1197+
1198+
.. code-block:: ruby
1199+
1200+
criteria = Tour.where('participants.name' => 'Serenity',).
1201+
unwind(:states).
1202+
group(_id: 'states', :states.add_to_set => '$states').
1203+
project(_id: 0, states: 1)
1204+
1205+
pp criteria.pipeline
1206+
# => [{"$match"=>{"participants.name"=>"Serenity"}},
1207+
# {"$unwind"=>"$states"},
1208+
# {"$group"=>{"_id"=>"states", "states"=>{"$addToSet"=>"$states"}}},
1209+
# {"$project"=>{"_id"=>0, "states"=>1}}]
1210+
1211+
Tour.collection.aggregate(criteria.pipeline).to_a
1212+
1213+
group
1214+
~~~~~
1215+
1216+
The ``group`` method adds a `$group aggregation pipeline stage
1217+
<https://docs.mongodb.com/manual/reference/operator/aggregation/group/>`_.
1218+
1219+
The field expressions support Mongoid symbol-operator syntax:
1220+
1221+
.. code-block:: ruby
1222+
1223+
criteria = Tour.all.group(_id: 'states', :states.add_to_set => '$states')
1224+
criteria.pipeline
1225+
# => [{"$group"=>{"_id"=>"states", "states"=>{"$addToSet"=>"$states"}}}]
1226+
1227+
Alternatively, standard MongoDB aggregation pipeline syntax may be used:
1228+
1229+
.. code-block:: ruby
1230+
1231+
criteria = Tour.all.group(_id: 'states', states: {'$addtoSet' => '$states'})
1232+
1233+
project
1234+
~~~~~~~
1235+
1236+
The ``project`` method adds a `$project aggregation pipeline stage
1237+
<https://docs.mongodb.com/manual/reference/operator/aggregation/project/>`_.
1238+
1239+
The argument should be a Hash specifying the projection:
1240+
1241+
.. code-block:: ruby
1242+
1243+
criteria = Tour.all.project(_id: 0, states: 1)
1244+
criteria.pipeline
1245+
# => [{"$project"=>{"_id"=>0, "states"=>1}}]
1246+
1247+
.. _unwind-dsl:
1248+
1249+
unwind
1250+
~~~~~~
1251+
1252+
The ``unwind`` method adds an `$unwind aggregation pipeline stage
1253+
<https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/>`_.
1254+
1255+
The argument can be a field name, specifyable as a symbol or a string, or
1256+
a Hash or a ``BSON::Document`` instance:
1257+
1258+
.. code-block:: ruby
1259+
1260+
criteria = Tour.all.unwind(:states)
1261+
criteria = Tour.all.unwind('states')
1262+
criteria.pipeline
1263+
# => [{"$unwind"=>"$states"}]
1264+
1265+
criteria = Tour.all.unwind(path: '$states')
1266+
criteria.pipeline
1267+
# => [{"$unwind"=>{:path=>"$states"}}]

0 commit comments

Comments
 (0)