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
0 commit comments