|
| 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 representation of a MongoDB |
| 25 | +document. To learn more about the terminology, structure, and limitations of |
| 26 | +MongoDB documents, see :manual:`Documents </core/document/>` in the |
| 27 | +{+server-manual+}. |
| 28 | + |
| 29 | +You must include the ``Mongoid::Document`` module in any class that you |
| 30 | +want to persist to MongoDB. By including the ``Document`` module in your |
| 31 | +model class, you can use its methods on instances of your model class. |
| 32 | + |
| 33 | +The following code demonstrates how to include the ``Document`` module |
| 34 | +in a sample ``Person`` model class: |
| 35 | + |
| 36 | +.. code-block:: ruby |
| 37 | + :emphasize-lines: 2 |
| 38 | + |
| 39 | + class Person |
| 40 | + include Mongoid::Document |
| 41 | + |
| 42 | + field :name, type: String |
| 43 | + end |
| 44 | + |
| 45 | +You can find more information about the ``Document`` module in the `API |
| 46 | +documentation <{+api-root+}/Document.html>`__. |
| 47 | + |
| 48 | +MongoDB Representation |
| 49 | +---------------------- |
| 50 | + |
| 51 | +The representation of a ``Document`` in MongoDB is a BSON object that is |
| 52 | +similar to a {+language+} hash or JSON object. You can store instances |
| 53 | +of your models directly in a collection in the database, or you can |
| 54 | +embed them in other classes that use the ``Document`` module. |
| 55 | + |
| 56 | +To learn more about how to model your data by using {+odm+} models, |
| 57 | +see the :ref:`mongoid-data-modeling` guides. |
| 58 | + |
| 59 | +.. TODO Add link to field types guide. |
0 commit comments