-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-45358: documents #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
.. _mongoid-modeling-documents: | ||
|
||
========= | ||
Documents | ||
========= | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: ruby framework, odm, code example, bson | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn about the ``Mongoid::Document`` module in | ||
{+odm+}. The ``Document`` module is a {+language+} implementation of a | ||
MongoDB document, which stores data in field-and-value pairs. To learn | ||
more about the terminology, structure, and limitations of | ||
MongoDB documents, see :manual:`Documents </core/document/>` in the | ||
{+server-manual+}. | ||
|
||
You must include the ``Mongoid::Document`` module in any class that you | ||
want to persist to MongoDB. By including the ``Document`` module in your | ||
model class, you can use its methods on instances of your model class. | ||
|
||
The following code demonstrates how to include the ``Document`` module | ||
in a sample ``Person`` model class: | ||
|
||
.. code-block:: ruby | ||
:emphasize-lines: 2 | ||
|
||
class Person | ||
include Mongoid::Document | ||
|
||
field :name, type: String | ||
end | ||
|
||
You can find more information about the ``Document`` module in the `API | ||
documentation <{+api-root+}/Document.html>`__. | ||
|
||
Work with Documents | ||
------------------- | ||
|
||
You can store instances of your models directly in a collection, or you | ||
can embed them in other classes that use the ``Document`` module. | ||
When you save a ``Document`` instance to MongoDB, it is converted | ||
to a BSON object that is similar to a {+language+} hash or JSON | ||
object. | ||
|
||
The following code creates an instance of the ``Person`` model defined | ||
in the preceding section: | ||
|
||
.. code-block:: ruby | ||
|
||
Person.create(name: 'Meena Kumar') | ||
|
||
The document appears in MongoDB as follows: | ||
|
||
.. code-block:: json | ||
:copyable: false | ||
|
||
{ | ||
"_id": { | ||
"$oid": "673b6dce61700598c24a72b0" | ||
}, | ||
"name": "Meena Kumar" | ||
} | ||
|
||
.. note:: _id Field | ||
|
||
When you persist an instance of a model to the database, MongoDB | ||
automatically adds an ``_id`` field that has a unique value even if you | ||
do not explicitly define this field in your model. | ||
|
||
To learn more about this field, see the :manual:`ObjectId reference | ||
</reference/bson-types/#objectid>` in the {+server-manual+}. | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
To learn how to access and change your MongoDB data, see the | ||
:ref:`mongoid-interact-data` guides. | ||
|
||
To learn more about how to model your data by using {+odm+} models, | ||
see the :ref:`mongoid-data-modeling` guides. | ||
|
||
.. TODO Add link to field types guide. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm assuming this guide hasn't been created yet, but pointing this out just in case it has! |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.