|
| 1 | +.. _mongoid-modeling-documents: |
| 2 | + |
| 3 | +========= |
| 4 | +Documents |
| 5 | +========= |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: ruby framework, odm, code example, bson |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn about the ``Mongoid::Document`` module in |
| 24 | +{+odm+}. The ``Document`` module is a {+language+} implementation of a |
| 25 | +MongoDB document, which stores data in field-and-value pairs. To learn |
| 26 | +more about the terminology, structure, and limitations of |
| 27 | +MongoDB documents, see :manual:`Documents </core/document/>` in the |
| 28 | +{+server-manual+}. |
| 29 | + |
| 30 | +You must include the ``Mongoid::Document`` module in any class that you |
| 31 | +want to persist to MongoDB. By including the ``Document`` module in your |
| 32 | +model class, you can use its methods on instances of your model class. |
| 33 | + |
| 34 | +The following code demonstrates how to include the ``Document`` module |
| 35 | +in a sample ``Person`` model class: |
| 36 | + |
| 37 | +.. code-block:: ruby |
| 38 | + :emphasize-lines: 2 |
| 39 | + |
| 40 | + class Person |
| 41 | + include Mongoid::Document |
| 42 | + |
| 43 | + field :name, type: String |
| 44 | + end |
| 45 | + |
| 46 | +You can find more information about the ``Document`` module in the `API |
| 47 | +documentation <{+api-root+}/Document.html>`__. |
| 48 | + |
| 49 | +Work with Documents |
| 50 | +------------------- |
| 51 | + |
| 52 | +You can store instances of your models directly in a collection, or you |
| 53 | +can embed them in other classes that use the ``Document`` module. |
| 54 | +When you save a ``Document`` instance to MongoDB, it is converted |
| 55 | +to a BSON object that is similar to a {+language+} hash or JSON |
| 56 | +object. |
| 57 | + |
| 58 | +The following code creates an instance of the ``Person`` model defined |
| 59 | +in the preceding section: |
| 60 | + |
| 61 | +.. code-block:: ruby |
| 62 | + |
| 63 | + Person.create(name: 'Meena Kumar') |
| 64 | + |
| 65 | +The document appears in MongoDB as follows: |
| 66 | + |
| 67 | +.. code-block:: json |
| 68 | + :copyable: false |
| 69 | + |
| 70 | + { |
| 71 | + "_id": { |
| 72 | + "$oid": "673b6dce61700598c24a72b0" |
| 73 | + }, |
| 74 | + "name": "Meena Kumar" |
| 75 | + } |
| 76 | + |
| 77 | +.. note:: _id Field |
| 78 | + |
| 79 | + When you persist an instance of a model to the database, MongoDB |
| 80 | + automatically adds an ``_id`` field that has a unique value even if you |
| 81 | + do not explicitly define this field in your model. |
| 82 | + |
| 83 | + To learn more about this field, see the :manual:`ObjectId reference |
| 84 | + </reference/bson-types/#objectid>` in the {+server-manual+}. |
| 85 | + |
| 86 | +Additional Information |
| 87 | +---------------------- |
| 88 | + |
| 89 | +To learn how to access and change your MongoDB data, see the |
| 90 | +:ref:`mongoid-interact-data` guides. |
| 91 | + |
| 92 | +To learn more about how to model your data by using {+odm+} models, |
| 93 | +see the :ref:`mongoid-data-modeling` guides. |
| 94 | + |
| 95 | +.. TODO Add link to field types guide. |
0 commit comments