AggregationCursor
AggregationCursor()
AggregationCursor.prototype.addCursorFlag()
AggregationCursor.prototype.close()
AggregationCursor.prototype.eachAsync()
AggregationCursor.prototype.map()
AggregationCursor.prototype.next()
AggregationCursor.prototype[Symbol.asyncIterator]()
AggregationCursor()
參數 (Parameters)
agg
«Aggregate»
繼承自
AggregationCursor 是一個並行處理基本元件,用於一次處理一個聚合結果文件。它類似於 QueryCursor。
AggregationCursor 符合 Node.js streams3 API,此外還有其他機制可以一次從 MongoDB 加載文件。
建立 AggregationCursor 會執行模型的 pre aggregate 鉤子,但不會執行模型的 post aggregate 鉤子。
除非您是進階使用者,否則請不要直接實例化此類別。請改用 Aggregate#cursor()
。
AggregationCursor.prototype.addCursorFlag()
參數 (Parameters)
flag
«String»value
«Boolean»
返回
- «AggregationCursor» this
新增一個游標旗標。適用於設定 noCursorTimeout
和 tailable
旗標。
AggregationCursor.prototype.close()
參數 (Parameters)
callback
«Function»
返回
- «Promise»
請參閱
將此游標標記為已關閉。將停止串流,並且後續呼叫 next()
將會發生錯誤。
AggregationCursor.prototype.eachAsync()
參數 (Parameters)
fn
«Function»[options]
«Object»[options.parallel]
«Number» 並行執行的 promise 數量。預設值為 1。[options.batchSize=null]
«Number» 如果設定,Mongoose 將使用最多batchSize
個文件的陣列呼叫fn
,而不是單個文件[options.continueOnError=false]
«Boolean» 如果為 true,即使fn
擲回錯誤,eachAsync()
也會迭代所有文件。如果為 false,如果給定的函式fn()
擲回錯誤,eachAsync()
會立即擲回錯誤。
返回
- «Promise»
為游標中的每個文件執行 fn
。如果 fn
返回一個 promise,則會等待 promise 完成後再迭代到下一個 promise。返回一個在完成時解析的 promise。
AggregationCursor.prototype.map()
參數 (Parameters)
fn
«Function»
返回
- «AggregationCursor»
註冊一個轉換函式,該函式隨後映射透過串流介面或 .next()
檢索的文件
範例
// Map documents returned by `data` events
Thing.
find({ name: /^hello/ }).
cursor().
map(function (doc) {
doc.foo = "bar";
return doc;
})
on('data', function(doc) { console.log(doc.foo); });
// Or map documents returned by `.next()`
const cursor = Thing.find({ name: /^hello/ }).
cursor().
map(function (doc) {
doc.foo = "bar";
return doc;
});
cursor.next(function(error, doc) {
console.log(doc.foo);
});
AggregationCursor.prototype.next()
返回
- «Promise»
從此游標取得下一個文件。當沒有剩餘文件時,將返回 null
。
AggregationCursor.prototype[Symbol.asyncIterator]()
返回一個 asyncIterator,用於 for/await/of
迴圈。您不需要明確呼叫此函式,JavaScript 執行階段會為您呼叫它。
範例
// Async iterator without explicitly calling `cursor()`. Mongoose still
// creates an AggregationCursor instance internally.
const agg = Model.aggregate([{ $match: { age: { $gte: 25 } } }]);
for await (const doc of agg) {
console.log(doc.name);
}
// You can also use an AggregationCursor instance for async iteration
const cursor = Model.aggregate([{ $match: { age: { $gte: 25 } } }]).cursor();
for await (const doc of cursor) {
console.log(doc.name);
}
Node.js 10.x 原生支援 async iterators,無需任何旗標。您可以使用 --harmony_async_iteration
旗標在 Node.js 8.x 中啟用 async iterators。
注意:如果未定義 Symbol.asyncIterator
,則不會設定此函式。如果未定義 Symbol.asyncIterator
,則表示您的 Node.js 版本不支援 async iterators。