π Empty Aggregation Pipeline
An empty aggregationβ
This code is the equivalent of a SELECT * FROM AUTHORS
. It returns a cursor with all documents in the authors
collection:
- Atlas UI
- MongoDB Shell
- Make sure
Collections
>authors
is selected. - Open the
Aggregation
tab. - Select
Text
. - Notice the empty array in the editor denoting an empty aggregation pipeline:
[]
You're getting all documents in authors
(although the Atlas UI is only showing a 10 documents sample)
db.authors.aggregate([])
We can iterate over the returned cursor and get more documents by typing it
.
π Return all the documents in the books
collection and iterate to get the next page of books.
Answer
db.books.aggregate([])
it
π¦ΈββοΈ Cursor methodsβ
info
Extra activity! Do it if you have extra time or are following along at home. It won't be covered during the hands-on lab.
A cursor has several useful methods. For instance, we can check the size of the returned cursor with itcount
.
cursor.itcount()
π In our previous example with the empty aggregation, to check the size of the returned cursor, what should we type in?
Answer
// as db.books.aggregate([]) returns a cursor we can just call itcount() on it
db.books.aggregate([]).itcount()