Skip to content

DOCSP-46322: Raw queries #1

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 15 commits into from
Jan 24, 2025
5 changes: 4 additions & 1 deletion snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
"https://www.mongodb.com/docs/atlas/objects.inv"
]

# toc_landing_pages = ["/paths/to/pages/that/have/nested/content"]
toc_landing_pages = [
"/interact-data"
]

[constants]
django-odm = "Django MongoDB Backend"
api = "https://django-mongodb.readthedocs.io/en/latest/"
mdb-server = "MongoDB Server"
django-version = "5.0"
django-docs = "https://docs.djangoproject.com/en/{+django-version+}"
pymongo-docs = "https://www.mongodb.com/docs/languages/python/pymongo-driver/current/"
106 changes: 106 additions & 0 deletions source/includes/interact-data/raw-queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# start-models
from django.db import models
from django_mongodb_backend.fields import ArrayField
from django_mongodb_backend.managers import MongoManager

class Movie(models.Model):
title = models.CharField(max_length=200)
plot = models.TextField(blank=True)
runtime = models.IntegerField(default=0)
released = models.DateTimeField("release date", null=True, blank=True)
genres = ArrayField(models.CharField(max_length=100), null=True, blank=True)
objects = MongoManager()

class Meta:
db_table = "movies"
managed = False

def __str__(self):
return self.title

class Theater(models.Model):
theaterId = models.IntegerField(default=0)
objects = MongoManager()

class Meta:
db_table = "theaters"
managed = False

def __str__(self):
return self.title
# end-models

# start-filter-project
from sample_mflix.models import Movie

movies = Movie.objects.raw_aggregate([
{"$match": {"title": "The Parent Trap"}},
{"$project": {
"title": 1,
"released": 1
}
}])

for m in movies:
print(f"Plot of {m.title}, released on {m.released}: {m.plot}\n")
# end-filter-project

# start-atlas-search
movies = Movie.objects.raw_aggregate([
{
"$search": {
"index": "<search-index-name>",
"phrase": {
"path": "plot",
"query": "whirlwind romance",
"slop": 3
},
"highlight": {
"path": "plot"
}
}
},
{
"$project": {
"title": 1,
"highlight": {"$meta": "searchHighlights"}
}
}
])

for m in movies:
print(f"Title: {m.title}, text match details: {m.highlight}\n")
# end-atlas-search

# start-geo
chicago_bounds = {
"type": "Polygon",
"coordinates": [[
[-87.851, 41.976],
[-87.851, 41.653],
[-87.651, 41.653],
[-87.651, 41.976],
[-87.851, 41.976]
]]
}

theaters = Theater.objects.raw_aggregate([
{
"$match": {
"location.geo": {
"$geoWithin": {
"$geometry": chicago_bounds
}
}
}
},
{
"$project": {
"theaterId": 1
}
}
])

for t in theaters:
print(f"Theater ID: {t.theaterId}")
# end-geo
14 changes: 7 additions & 7 deletions source/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Django MongoDB Backend

.. toctree::

Interact with Data </interact-data>
Issues & Help </issues-and-help>
Compatibility </compatibility>


.. TODO:
Get Started </get-started>
Connection Configuration </connect>
Interact with Data </interact-data>
Model Your Data </model-data>
Django Feature Limitations </feature-limitations>
API Documentation <{+api+}>
Expand All @@ -29,6 +29,12 @@ Introduction
Welcome to the documentation site for the official {+django-odm+},
a Django database backend that uses PyMongo to connect to MongoDB.

Interact with Data
------------------

Learn how to use {+django-odm+} to perform operations on MongoDB data
in the :ref:`django-interact-data` section.

.. TODO:

.. Get Started
Expand All @@ -43,12 +49,6 @@ a Django database backend that uses PyMongo to connect to MongoDB.
.. Learn how to configure a connection to a MongoDB deployment
in the :ref:`django-connection-configuration` section.

.. Interact with Data
.. ------------------

.. Learn how to use {+django-odm+} to perform operations on MongoDB data
in the :ref:`django-interact-data` section.

.. Model Your Data
.. ---------------

Expand Down
17 changes: 17 additions & 0 deletions source/interact-data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.. _django-interact-data:

==================
Interact with Data
==================

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: odm, crud, query

.. toctree::
:caption: Interact with Data

Perform Raw Queries </interact-data/raw-queries>
Loading
Loading