Cursor defaults are access(1), batchSize(100), grant(null), maxTimeMs(2000), skipAcl(false), wrap(false).
access(level)
batchSize(sz)
close()
forEach(fn)
grant(level = null)
group(object)
hasNext()
isClosed()
limit(count)
map(fn)
match(object)
maxTimeMS(ms)
next()
project(object)
reduce(memo, fn)
skip(count)
skipAcl(bool = true)
sort(object)
stream(fnEach, fnMap)
toArray()
toList()
unwind(prop)
wrap(bool = true)
const cursor = org.objects.account
.aggregate()
.skipAcl()
.grant(consts.accessLevels.read)
.sort({
'name.first': 1
})
.project({
_id: 1,
'name.first': 1
})
.limit(50);
const first_names = [];
for (let account of cursor) {
first_names.push( account.name.first );
}
return first_names;
// OR return cursor.map(v => v.name.first)
const cursor = org.objects.account
.aggregate()
.skipAcl()
.grant(consts.accessLevels.read)
.sort({
'name.first': 1
})
.project({
_id: 1,
first_name: 'name.first'
})
.limit(50)
.stream(); // stream json directly to the response object.
AggregationCursor.access(level)
Sets the minimum context access level the caller needs to return results. To return only results to which the caller has update access, for example, call cursor.access(consts.accessLevels.update)
.
Arguments
level
(Number)
Returns
this
AggregationCursor.batchSize(sz)
Sets the internal cursor batch size for fetching results. Normally, the default is optimal but in cases where a large number of very small documents is being fetched, a larger batch size may be slightly more performant.
Arguments
sz
(Number) The internal cursor batch size (between 1 and 1000).
Returns
this
AggregationCursor.close()
Releases the cursor resources. It's not necessary to close cursors as this is done automatically when a script ends or is exhausted, but in some cases a script may need to free up a cursor mid-stream in order to open up another one.
Returns
this
AggregationCursor.forEach(fn)
Executes the cursor, calling fn
with each document.
Arguments
fn
(Function) a function to call for each element in the form of(document) => {}
.
Returns
undefined
AggregationCursor.grant(level = null)
Sets the property grant level for the operation.
Arguments
level
(Number)
Returns
this
AggregationCursor.group(object)
Adds a $group stage to the aggregation.
Arguments
object
(Object)
Returns
this
AggregationCursor.hasNext()
Executes the cursor, returning true if next()
can be safely called to fetch the next result.
Returns
bool
AggregationCursor.isClosed()
Returns true if a cursor has been opened and has been subsequently closed or exhausted.
Returns
Boolean
AggregationCursor.limit(count)
Adds a $limit stage to the aggregation. By default, the script toList()
limit is 10, though cursors have no default limit.
Arguments
count
(Number)
Returns
this
AggregationCursor.map(fn)
Exhausts the cursor, calling fn
with each document, and returning an array of results. Because map()
iterates through the cursor, the caller should set a sane limit()
.
Arguments
fn
(Function) a function to call for each result in the form of(document) => { return document; }
.
Returns
Object[]
AggregationCursor.match(object)
Adds a $match stage to the aggregation.
Arguments
object
(Object)
Returns
this
AggregationCursor.maxTimeMs(ms)
Set the maximum time a query is allowed to run.
Arguments
ms
(Number) A number between 10 and 10000.
Returns
this
AggregationCursor.next()
Returns the next result. Throws a RangeError is there are no more results. Use while ( cursor.hasNext() ) { cursor.next() }
or for result of cursor
loops to iterate cursors.
Returns
Object
AggregationCursor.project(object)
Adds a $project stage to the aggregation.
Arguments
object
(Object)
Returns
this
AggregationCursor.reduce(fn, memo)
Exhausts the cursor, calling fn
with memo for each document, and returning memo
. Because reduce()
iterates through the cursor, the caller should set a sane limit()
.
Arguments
fn
(Function) a function to call for each result in the form of(memo, document) => { return memo; }
.memo
(Object) an initial value forfn
.
Returns
Object
AggregationCursor.skip(count)
Skips count
number of results. Please not that large offsets can be expensive and slow down your query. It's more efficient to use range queries.
Arguments
count
(Number) The number of results to skip.
Returns
this
AggregationCursor.skipAcl(bool = true)
Skips initial lookup access control checks. Use in conjunction with grant()
to set property level access.
Arguments
bool
(Boolean)
Returns
this
AggregationCursor.sort(object)
Adds a $sort stage to the aggregation.
Arguments
object
(Object)
Returns
this
AggregationCursor.stream(fnEach, fnMap)
Exhausts the cursor, setting the content type to application/json
and writing directly to the Response as a list object. Values returned from the script after stream is called are ignored.
Arguments
fnEach
(Function) optional function -(result, total) => { return false; }
- called after each item is written. Returningfalse
ends the stream early.fnMap
(Function) optional function -(result) => { return result; }
- called to transform each result before output, the result of which is JSON.stringify()'d.
Returns
undefined
AggregationCursor.toArray()
Exhausts the cursor and returns an array of results (not a list! see toList() ). Because toArray()
iterates through the cursor, the caller should set a sane limit()
.
Returns
Object[]
AggregationCursor.toList()
Returns a list object based on limit()
(or the default limit of 10).
Returns
List ({object: 'list', data: [...], hasMore: true}
)
AggregationCursor.unwind(prop)
Adds an $unwind stage to the aggregation.
Arguments
prop
(String) the property to unwind.
Returns
this
AggregationCursor.wrap(bool = true)
Where applicable, returns results as CortexObject instances instead of plain Javascript objects.
Arguments
bool
(Boolean)
Returns
this