@@ -193,45 +193,47 @@ Learn [when to use $addFields over $project](https://www.practical-mongodb-aggre
193193 <TabItem value = " project" label = " Using $project" >
194194 <div >
195195 ``` python
196- most_authors = books.aggregate([
196+ most_authors = books.aggregate
197+ ([
197198 { " $match" : { " year" : { " $gt" : 2000 } } },
198- { " $match" : { " authors" : { " $exists" : True } } },
199- {
200- " $project" : {
201- " title" : 1 ,
202- " year" : 1 ,
203- " authors" : 1 ,
204- " numAuthors" : { " $size" : " $authors" }
205- }
206- },
207- { " $sort" : { " numAuthors" : - 1 } },
208- { " $limit" : 1 }
209- ])
210-
211- for book in most_authors:
212- print (book)
213- ```
199+ { " $match" : { " authors" : { " $exists" : True } } },
200+ {
201+ " $project" : {
202+ " title" : 1 ,
203+ " year" : 1 ,
204+ " authors" : 1 ,
205+ " numAuthors" : { " $size" : " $authors" }
206+ }
207+ },
208+ { " $sort" : { " numAuthors" : - 1 } },
209+ { " $limit" : 1 }
210+ ])
211+
212+ for book in most_authors:
213+ print (book)
214+ ```
214215 </div >
215216 </TabItem >
216217
217218 <TabItem value = " addFields" label = " Using $addFields" >
218219 <div >
219220 ``` python
220- most_authors = books.aggregate([
221+ most_authors = books.aggregate
222+ ([
221223 { " $match" : { " year" : { " $gt" : 2000 } } },
222- { " $match" : { " authors" : { " $exists" : True } } },
223- {
224- " $addFields" : {
225- " numAuthors" : { " $size" : " $authors" }
226- }
227- },
228- { " $sort" : { " numAuthors" : - 1 } },
229- { " $limit" : 1 }
230- ])
231-
232- for book in most_authors:
233- print (book)
234- ```
224+ { " $match" : { " authors" : { " $exists" : True } } },
225+ {
226+ " $addFields" : {
227+ " numAuthors" : { " $size" : " $authors" }
228+ }
229+ },
230+ { " $sort" : { " numAuthors" : - 1 } },
231+ { " $limit" : 1 }
232+ ])
233+
234+ for book in most_authors:
235+ print (book)
236+ ```
235237 </div >
236238 </TabItem >
237239 </Tabs >
0 commit comments