Skip to content

DOCSP-46321: CRUD operations #144

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

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions source/interact-data/crud.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ create, read, update, and delete (CRUD) operations on your MongoDB
collection.

You can use methods provided by the Django ``QuerySet`` API to run
CRUD operations. To update documents in your collection, call the
``QuerySet`` operation methods on your model objects that represent the collection.
Then, {+django-odm+} runs the operations on your collection documents.
CRUD operations. To run these operations, call ``QuerySet`` methods

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operations, ^you can call

your model's ``Manager``. The ``Manager`` class handles database

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

methods ^on your

operations and allows you to interact with your MongoDB data by referencing
Django models. By default, Django adds a ``Manager`` named ``objects``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: consider making just "Django" a source constant as its a word with a high possibility of typos, IMO

to every model class.

This guide shows how to use the following ``QuerySet`` methods:

- :ref:`create() <django-crud-insert>`
Expand Down Expand Up @@ -57,7 +60,6 @@ The ``Movie`` model class has the following definition:

from django.db import models
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField
from django_mongodb_backend.managers import MongoManager

class Movie(models.Model):
title = models.CharField(max_length=200)
Expand All @@ -66,7 +68,6 @@ The ``Movie`` model class has the following definition:
released = models.DateTimeField("release date", null=True, blank=True)
awards = EmbeddedModelField(Award)
genres = ArrayField(models.CharField(max_length=100), null=True, blank=True)
objects = MongoManager()

class Meta:
db_table = "movies"
Expand All @@ -83,6 +84,15 @@ root directory:

python manage.py shell

After entering the Python shell, ensure that you import the following models and
modules:

.. code-block:: python

from <your application name>.models import Movie
from django.utils import timezone
from datetime import datetime

To learn how to create a Django application that uses the ``Movie``
model and the Python interactive shell to interact with MongoDB documents,
visit the :ref:`django-get-started` tutorial.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: link broken until #132 is merged

Expand All @@ -92,16 +102,16 @@ visit the :ref:`django-get-started` tutorial.
Insert Documents
----------------

To insert a document into a collection, call the ``create()`` method on your
model objects that represent the collection. Pass the new document's fields
and values as arguments to the ``create()`` method.
To insert a document into a collection, call the ``create()`` method
on an instance of your model's ``Manager`` class. Pass the new document's
fields and values as arguments to the ``create()`` method.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To insert a document into a collection, call the create() method on your model's Manager class. Pass the new document's field names and field values as arguments to the create() method.

Example
~~~~~~~

The following example calls the ``create()`` method on your ``Movie`` objects
to insert a document into the ``sample_mflix.movies`` collection. The new
document has a ``title`` value of ``"Poor Things"`` and a ``runtime`` value
The following example calls the ``create()`` method to insert a document
into the ``sample_mflix.movies`` collection. The new document has
a ``title`` value of ``"Poor Things"`` and a ``runtime`` value
of ``141``:

.. code-block:: python
Expand All @@ -123,8 +133,8 @@ of ``141``:
Read Documents
--------------

To retrieve documents from your collection, call the ``filter()`` method on your
model objects that represent the collection. Pass a query filter, or criteria
To retrieve documents from your collection, call the ``filter()`` method on an
instance of your model's ``Manager`` class. Pass a query filter, or criteria
that specifies which documents to retrieve, as an argument to the ``filter()`` method.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To retrieve documents from your collection, call the filter() method on your model's Manager class.


Alternatively, you can call the ``get()`` method to retrieve a single document
Expand All @@ -133,8 +143,8 @@ that matches your query.
Return Multiple Documents Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following example calls the ``filter()`` method on your ``Movie`` objects
to retrieve documents from the ``sample_mflix.movies`` collection. The query
The following example calls the ``filter()`` method to retrieve
documents from the ``sample_mflix.movies`` collection. The query
returns ``Movie`` objects that represent movies released on January 1, 2000:

.. io-code-block::
Expand Down Expand Up @@ -192,16 +202,16 @@ Modify Documents
----------------

To modify documents in a collection, call the ``filter()`` and ``update()``
methods on your model objects that represent the collection. Pass a query filter,
methods on an instance of your model's ``Manager`` class. Pass a query filter,
or criteria that specifies which documents to update, as an argument to the
``filter()`` method. Then, pass the fields and values you want to update as
arguments to the ``update()`` method.

Example
~~~~~~~

The following example calls the ``update()`` method on your ``Movie`` objects
to modify documents in the ``sample_mflix.movies`` collection. The code matches
The following example calls the ``update()`` method to modify
documents in the ``sample_mflix.movies`` collection. The code matches
a document that has a ``title`` value of ``"High Fidelity"`` and adds a
``plot`` field:

Expand Down Expand Up @@ -229,15 +239,15 @@ Delete Documents
----------------

To delete documents in a collection, call the ``filter()`` and ``delete()``
methods on your model objects that represent the collection. Pass a query filter,
methods on an instance of your model's ``Manager`` class. Pass a query filter,
or criteria that specifies which documents to delete, as an argument to the
``filter()`` method.

Example
~~~~~~~

The following example calls the ``delete()`` method on your ``Movie`` objects
to delete documents in the ``sample_mflix.movies`` collection. The code matches
The following example calls the ``delete()`` method to delete documents
in the ``sample_mflix.movies`` collection. The code matches
and deletes documents that have a ``runtime`` value of ``5``:

.. io-code-block::
Expand Down
Loading