- Stitch >
- MongoDB Atlas >
- Reference >
- MongoDB Actions
collection.aggregate()¶
On this page
Definition¶
-
collection.
aggregate
()¶
Execute an aggregation pipeline and return a handle object that allows you to access the pipeline’s output documents.
Usage¶
Example¶
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
To call the collection.aggregate()
action from a
Function, get a collection handle with
database.collection()
then call the handle’s
aggregate()
method.
To call the collection.aggregate()
action from a JavaScript
SDK, use the RemoteMongoCollection.aggregate() method.
To call the collection.aggregate()
action from the
Java/Android SDK, use the RemoteMongoCollection.aggregate() method.
To call the collection.aggregate()
action from the
Swift/iOS SDK, use the RemoteMongoCollection.aggregate() method.
Parameters¶
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
The collection.aggregate()
action has the following form:
The aggregate() method has the following form:
The aggregate() method has the following form:
The aggregate() method has the following form:
Parameter | Description |
---|---|
Aggregation Pipeline pipeline: Array<document> |
An array of one or more aggregation pipeline stage documents. Supported Aggregation Stages Stitch supports nearly all MongoDB aggregation pipeline stages and operators, but some stages must be executed within a system function. See Aggregation Framework Limitations for more information. |
Return Value¶
- Functions
- JavaScript SDK
- Android SDK
- iOS SDK
The collection.aggregate()
action returns a cursor object
that points to any documents output from the final stage of the
aggregation pipeline. You can manipulate and access documents
in the aggregation result set with the following methods:
Method | Description |
---|---|
cursor.next() |
Iterates the cursor and returns a Promise that resolves to the
next document in the cursor. If the cursor is exhausted, the
promise resolves to Example |
cursor.toArray() |
Iterates the cursor to exhaustion and returns a Promise that resolves to an array that contains all of the iterated documents. Example |
Note
You cannot return a cursor from a Function. Instead, evaluate the cursor using
cursor.next()
or cursor.toArray()
and return the
result.
The RemoteMongoCollection.aggregate() method returns a RemoteMongoReadOperation object.
You can access documents in the aggregation result set by
calling one of the following methods on the
RemoteMongoReadOperation
object:
Method | Description |
---|---|
First Document result.first() |
“” Return a Promise that resolves to the first document from the query result set. Return a StitchResult enum to the completion handler that resolves to the first document from the query result set. See RemoteMongoReadOperation.first(). |
Array of Documents result.toArray() |
“” Return a Promise that resolves to an array that contains all documents in the query result set. Return a StitchResult enum to the completion handler that resolves to an array that contains all documents in the query result set. See RemoteMongoReadOperation.toArray(). |
Query Cursor result.iterator() |
“” Return a Promise that resolves to a RemoteMongoCursor object. You can use this cursor to iterate through each document in the query result set with the RemoteMongoCursor.next() method. Return a StitchResult enum to the completion handler that resolves to a RemoteMongoCursor object. You can use this cursor to iterate through each document in the query result set with the RemoteMongoCursor.next() method. See RemoteMongoReadOperation.iterator(). |
The RemoteMongoCollection.aggregate() method returns a RemoteAggregateIterable object.
You can access documents in the aggregation result set by
calling one of the following methods on the
RemoteAggregateIterable
object:
Method | Description |
---|---|
First Document result.first() |
Return a Task that resolves to the first document from the query result set. |
Array of Documents result.into(<target>) |
Iterate all documents in the query result set into the target object. Return a Task that resolves to the populated target object. |
For Each result.forEach(<Block>) |
Iterate over the result set and apply the given block to each document. Does not return a value. |
Map Document Type result.map(<Function>) |
Return a new iterator that maps each |
Query Cursor result.iterator() |
Return a Task that resolves to a RemoteMongoCursor object. You can use this cursor to iterate through each document in the query result set with the RemoteMongoCursor.tryNext() method. |
The RemoteMongoCollection.aggregate() method returns a StitchResult enum to its completion handler:
If the find operation was successful, the StitchResult
resolves to a RemoteMongoReadOperation object that allows you to
access documents in the aggregation result set by calling one
of the following methods:
Method | Description |
---|---|
First Document result.first() |
“” Return a Promise that resolves to the first document from the query result set. Return a StitchResult enum to the completion handler that resolves to the first document from the query result set. See RemoteMongoReadOperation.first(). |
Array of Documents result.toArray() |
“” Return a Promise that resolves to an array that contains all documents in the query result set. Return a StitchResult enum to the completion handler that resolves to an array that contains all documents in the query result set. See RemoteMongoReadOperation.toArray(). |
Query Cursor result.iterator() |
“” Return a Promise that resolves to a RemoteMongoCursor object. You can use this cursor to iterate through each document in the query result set with the RemoteMongoCursor.next() method. Return a StitchResult enum to the completion handler that resolves to a RemoteMongoCursor object. You can use this cursor to iterate through each document in the query result set with the RemoteMongoCursor.next() method. See RemoteMongoReadOperation.iterator(). |