@@ -1075,6 +1075,78 @@ results from system collections.
10751075
10761076.. _additional-query-methods:
10771077
1078+ Finding By ``_id``
1079+ ------------------
1080+
1081+ Mongoid provides the ``find`` method on ``Criteria`` objects to find documents
1082+ by their ``_id`` values:
1083+
1084+ .. code-block:: ruby
1085+
1086+ Band.find('5f0e41d92c97a64a26aabd10')
1087+ # => #<Band _id: 5f0e41d92c97a64a26aabd10, name: "Juno Reactor">
1088+
1089+ The ``find`` method performs type conversion, if necessary, of the argument
1090+ to the type declared in the model being queried for the ``_id`` field.
1091+ By default, the ``_id`` type is ``BSON::ObjectId``, thus the query above
1092+ is equivalent to:
1093+
1094+ .. code-block:: ruby
1095+
1096+ Band.find(BSON::ObjectId.from_string('5f0e41d92c97a64a26aabd10'))
1097+ # => #<Band _id: 5f0e41d92c97a64a26aabd10, name: "Juno Reactor">
1098+
1099+ .. note::
1100+
1101+ When querying collections directly using the driver, type conversion is not
1102+ automatically performed:
1103+
1104+ .. code-block:: ruby
1105+
1106+ Band.collection.find(_id: BSON::ObjectId.from_string('5f0e41d92c97a64a26aabd10')).first
1107+ # => {"_id"=>BSON::ObjectId('5f0e41d92c97a64a26aabd10'), "name"=>"Juno Reactor"}
1108+
1109+ Band.collection.find(_id: '5f0e41d92c97a64a26aabd10').first
1110+ # => nil
1111+
1112+ The ``find`` method can accept multiple arguments, or an array of arguments.
1113+ In either case each of the arguments or array elements is taken to be an ``_id``
1114+ value, and documents with all of the specified ``_id`` values are returned in
1115+ an array:
1116+
1117+ .. code-block:: ruby
1118+
1119+ Band.find('5f0e41d92c97a64a26aabd10', '5f0e41b02c97a64a26aabd0e')
1120+ # => [#<Band _id: 5f0e41b02c97a64a26aabd0e, name: "SUN Project", description: nil, likes: nil>,
1121+ #<Band _id: 5f0e41d92c97a64a26aabd10, name: "Juno Reactor", description: nil, likes: nil>]
1122+
1123+ Band.find(['5f0e41d92c97a64a26aabd10', '5f0e41b02c97a64a26aabd0e'])
1124+ # => [#<Band _id: 5f0e41b02c97a64a26aabd0e, name: "SUN Project", description: nil, likes: nil>,
1125+ #<Band _id: 5f0e41d92c97a64a26aabd10, name: "Juno Reactor", description: nil, likes: nil>]
1126+
1127+ If the same ``_id`` value is given more than once, the corresponding document
1128+ is only returned once:
1129+
1130+ .. code-block:: ruby
1131+
1132+ Band.find('5f0e41b02c97a64a26aabd0e', '5f0e41b02c97a64a26aabd0e')
1133+ # => [#<Band _id: 5f0e41b02c97a64a26aabd0e, name: "SUN Project", description: nil, likes: nil>]
1134+
1135+ The documents returned are *not* ordered, and may be returned in a different
1136+ order from the order of provided ``_id`` values, as illustrated in the above
1137+ examples.
1138+
1139+ If any of the ``_id`` values are not found in the database, the behavior of
1140+ ``find`` depends on the value of the ``raise_not_found_error`` configuration
1141+ option. If the option is set to ``true``, ``find`` raises
1142+ ``Mongoid::Errors::DocumentNotFound`` if any of the ``_id``s are not found.
1143+ If the option is set to ``false`` and ``find`` is given a single ``_id`` to
1144+ find and there is no matching document, ``find`` returns ``nil``. If the
1145+ option is set to ``false`` and ``find`` is given an array of ``_id``s to find
1146+ and some are not found, the return value is an array of documents that were
1147+ found (which could be empty if no documents were found at all).
1148+
1149+
10781150Additional Query Methods
10791151------------------------
10801152
@@ -1170,26 +1242,6 @@ Mongoid also has some helpful methods on criteria.
11701242 Band.exists?
11711243 Band.where(name: "Photek").exists?
11721244
1173- * - ``Criteria#find``
1174-
1175- *Find a document or multiple documents by their ids. If the*
1176- ``raise_not_found_error`` *configuration option is set to* ``true``
1177- *which is the default and any of the ids are not found, raise an error.
1178- If the* ``raise_not_found_error`` *configuration option is set to*
1179- ``false``, return an array of documents that have the specified ids.*
1180-
1181- -
1182- .. code-block:: ruby
1183-
1184- Band.find("4baa56f1230048567300485c")
1185- Band.find(
1186- "4baa56f1230048567300485c",
1187- "4baa56f1230048567300485d"
1188- )
1189- Band.where(name: "Photek").find(
1190- "4baa56f1230048567300485c"
1191- )
1192-
11931245 * - ``Criteria#find_by``
11941246
11951247 *Find a document by the provided attributes. If not found,
0 commit comments