Skip to content

Commit 59c37d2

Browse files
p-mongop
andauthored
MONGOID-5019 Document omitting _id field from embedded documents (#4931)
Co-authored-by: Oleg Pudeyev <oleg@bsdpower.com>
1 parent e943c38 commit 59c37d2

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

source/tutorials/mongoid-documents.txt

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -804,19 +804,58 @@ To define the field anyway, use the ``overwrite: true`` option:
804804
end
805805

806806

807+
.. _custom-id:
808+
807809
Custom Ids
808810
----------
809811

810-
For cases when you do not want to have ``BSON::ObjectId`` ids, you can override Mongoid's
811-
``_id`` field and set them to whatever you like.
812+
By default, Mongoid defines the ``_id`` field on documents to contain a
813+
``BSON::ObjcetId`` value which is automatically generated by Mongoid.
814+
815+
It is possible to replace the ``_id`` field definition to change the type
816+
of the ``_id`` values or have different default values:
812817

813818
.. code-block:: ruby
814819

815-
class Band
816-
include Mongoid::Document
817-
field :name, type: String
818-
field :_id, type: String, default: ->{ name }
819-
end
820+
class Band
821+
include Mongoid::Document
822+
field :name, type: String
823+
field :_id, type: String, default: ->{ name }
824+
end
825+
826+
It is possible to omit the default entirely:
827+
828+
.. code-block:: ruby
829+
830+
class Band
831+
include Mongoid::Document
832+
field :_id, type: String
833+
end
834+
835+
If the default on ``_id`` is omitted, and no ``_id`` value is provided by
836+
your application, Mongoid will persist the document without the ``_id``
837+
value and one will be assigned by the server. However, Mongoid will
838+
not automatically retrieve this value when the document is persisted - you
839+
must obtain the persisted value (and the complete persisted document) using
840+
other means:
841+
842+
.. code-block:: ruby
843+
844+
band = Band.create!
845+
=> #<Band _id: , >
846+
band.id
847+
=> nil
848+
band.reload
849+
# raises Mongoid::Errors::DocumentNotFound
850+
Band.last
851+
=> #<Band _id: 5fc681c22c97a6791f324b99, >
852+
853+
Omitting ``_id`` fields is more common in :ref:`embedded documents <omit-id>`.
854+
855+
Mongoid also defines the ``id`` field aliased to ``_id``. The ``id``
856+
alias can :ref:`be removed <unalias-id>` if desired (such as to integrate
857+
with systems that use the ``id`` field to store value different from ``_id``.
858+
820859

821860
.. _bson-symbol:
822861

source/tutorials/mongoid-relations.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,39 @@ The following deviations are known:
702702
In general, Mongoid adopts the behavior of current server versions and validates more strictly.
703703

704704

705+
.. _omit-id:
706+
707+
Omitting ``_id`` Fields
708+
-----------------------
709+
710+
By default, Mongoid adds an ``_id`` field to each embedded document. This
711+
permits easy referencing of and operations on the embedded documents.
712+
713+
These ``_id`` fields may be omitted to save storage space. To do so,
714+
:ref:`override the _id field definition in the child documents <custom-id>`
715+
and remove the default value:
716+
717+
.. code-block:: ruby
718+
719+
class Order
720+
include Mongoid::Document
721+
722+
embeds_many :line_items
723+
end
724+
725+
class LineItem
726+
include Mongoid::Document
727+
728+
embedded_in :order
729+
730+
field :_id, type: Object
731+
end
732+
733+
In the current version of Mongoid the field definition is required, but
734+
without a default value specified no value will be stored in the database.
735+
A future version of Mongoid may allow removing previously defined fields.
736+
737+
705738
Common Behavior
706739
===============
707740

0 commit comments

Comments
 (0)