Skip to content

Commit de5d19d

Browse files
committed
DOCSP-46322: Raw queries
1 parent 688a57d commit de5d19d

File tree

4 files changed

+424
-3
lines changed

4 files changed

+424
-3
lines changed

snooty.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
55
"https://www.mongodb.com/docs/atlas/objects.inv"
66
]
77

8-
# toc_landing_pages = ["/paths/to/pages/that/have/nested/content"]
8+
toc_landing_pages = [
9+
"/interact-data"
10+
]
911

10-
# [constants]
11-
# constant = "value"
12+
[constants]
13+
django-odm = "Django MongoDB Backend"
14+
django-api = "https://django-mongodb.readthedocs.io/en/latest/"
15+
mdb-server = "MongoDB Server"
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# start-models
2+
from django.db import models
3+
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField
4+
from django_mongodb_backend.managers import MongoManager
5+
6+
class Movie(models.Model):
7+
title = models.CharField(max_length=200)
8+
plot = models.TextField(null=True)
9+
runtime = models.IntegerField(default=0)
10+
released = models.DateTimeField("release date", null=True)
11+
awards = EmbeddedModelField(Award)
12+
genres = ArrayField(models.CharField(max_length=100), blank=True)
13+
objects = MongoManager()
14+
15+
class Meta:
16+
db_table = "movies"
17+
18+
def __str__(self):
19+
return self.title
20+
21+
class Theater(models.Model):
22+
theaterId = models.IntegerField(default=0)
23+
objects = MongoManager()
24+
25+
class Meta:
26+
db_table = "theaters"
27+
28+
def __str__(self):
29+
return self.title
30+
# end-models
31+
32+
# start-filter-project
33+
from sample_mflix.models import Movie
34+
35+
movies = Movie.objects.raw_aggregate([
36+
{"$match": {"title": "The Parent Trap"}},
37+
{"$project": {
38+
"title": 1,
39+
"released": 1
40+
}
41+
}])
42+
43+
for m in movies:
44+
print(f"Plot of {m.title}, released on {m.released}: {m.plot}\n")
45+
# end-filter-project
46+
47+
# start-atlas-search
48+
movies = Movie.objects.raw_aggregate([
49+
{
50+
"$search": {
51+
"index": "<search-index-name>",
52+
"phrase": {
53+
"path": "plot",
54+
"query": "whirlwind romance",
55+
"slop": 3
56+
},
57+
"highlight": {
58+
"path": "plot"
59+
}
60+
}
61+
},
62+
{
63+
"$project": {
64+
"title": 1,
65+
"highlight": {"$meta": "searchHighlights"}
66+
}
67+
}
68+
])
69+
70+
for m in movies:
71+
print(f"Title: {m.title}, text match details: {m.highlight}\n")
72+
# end-atlas-search
73+
74+
# start-geo
75+
chicago_bounds = {
76+
"type": "Polygon",
77+
"coordinates": [[
78+
[-87.851, 41.976],
79+
[-87.851, 41.653],
80+
[-87.651, 41.653],
81+
[-87.651, 41.976],
82+
[-87.851, 41.976]
83+
]]
84+
}
85+
86+
theaters = Theater.objects.raw_aggregate([
87+
{
88+
"$match": {
89+
"location.geo": {
90+
"$geoWithin": {
91+
"$geometry": chicago_bounds
92+
}
93+
}
94+
}
95+
},
96+
{
97+
"$project": {
98+
"theaterId": 1
99+
}
100+
}
101+
])
102+
103+
for t in theaters:
104+
print(f"Theater ID: {t.theaterId}")
105+
# end-geo

source/interact-data.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. _django-interact-data:
2+
3+
==================
4+
Interact with Data
5+
==================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: odm, crud, query
13+
14+
.. toctree::
15+
:caption: Interact with Data
16+
17+
Perform Raw Queries </interact-data/raw-queries>

0 commit comments

Comments
 (0)