Skip to content

Commit 4d87c40

Browse files
authored
Merge pull request #67 from rustagir/DOCSP-45358-docuents
DOCSP-45358: documents
2 parents 893e73e + 9192fed commit 4d87c40

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

snooty.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ toc_landing_pages = [
1010
"/quick-start-rails",
1111
"/quick-start-sinatra",
1212
"/interact-data",
13-
"/interact-data/specify-query"
13+
"/interact-data/specify-query",
14+
"/data-modeling"
1415
]
1516

1617
[constants]

source/data-modeling.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ Model Your Data
1414
.. toctree::
1515
:caption: Data Modeling
1616

17+
Documents </data-modeling/documents>
1718
Inheritance </data-modeling/inheritance>
1819

1920
In this section, you can learn how to model data in {+odm+}.
2021

22+
- :ref:`mongoid-modeling-documents`: Learn about the ``Document``
23+
module.
24+
2125
- :ref:`mongoid-modeling-inheritance`: Learn how to implement
2226
inheritance in your model classes.

source/data-modeling/documents.txt

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)