集約カーソル
AggregationCursor()
AggregationCursor.prototype.addCursorFlag()
AggregationCursor.prototype.close()
AggregationCursor.prototype.eachAsync()
AggregationCursor.prototype.map()
AggregationCursor.prototype.next()
AggregationCursor.prototype[Symbol.asyncIterator]()
AggregationCursor()
パラメータ
agg
«集約»
継承
集約カーソルは、集約結果を一度に1つのドキュメントずつ処理するための並行処理プリミティブです。クエリカーソルに類似しています。
集約カーソルは、MongoDBからドキュメントを1つずつロードするための他のいくつかのメカニズムに加えて、Node.js streams3 APIを満たします。
集約カーソルを作成すると、モデルの事前集約フックは実行されますが、モデルの事後集約フックは**実行されません**。
上級ユーザーでない限り、このクラスを直接インスタンス化**しないでください**。代わりにAggregate#cursor()
を使用してください。
AggregationCursor.prototype.addCursorFlag()
パラメータ
flag
«文字列»value
«真偽値»
戻り値
- «集約カーソル» this
カーソルフラグを追加します。 noCursorTimeout
およびtailable
フラグの設定に役立ちます。
AggregationCursor.prototype.close()
パラメータ
callback
«関数»
戻り値
- «Promise» (プロミス)
参照
このカーソルを閉じているとマークします。ストリーミングを停止し、後続のnext()
の呼び出しはエラーになります。
AggregationCursor.prototype.eachAsync()
パラメータ
fn
«関数»[options]
«オブジェクト»[options.parallel]
«数値» 並列に実行するプロミスの数。デフォルトは1です。[options.batchSize=null]
«数値» 設定されている場合、Mongooseは単一のドキュメントではなく、最大batchSize
個のドキュメントの配列を使用してfn
を呼び出します[options.continueOnError=false]
«真偽値» trueの場合、fn
がエラーをスローしてもeachAsync()
はすべてのドキュメントを反復処理します。 falseの場合、指定された関数fn()
がエラーをスローすると、eachAsync()
はすぐにエラーをスローします。
戻り値
- «Promise» (プロミス)
カーソルのすべてのドキュメントに対してfn
を実行します。 fn
がプロミスを返す場合、次のドキュメントに進む前にプロミスが解決されるまで待機します。完了時に解決されるプロミスを返します。
AggregationCursor.prototype.map()
パラメータ
fn
«関数»
戻り値
- «集約カーソル»
ストリームインターフェースまたは.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]()
for/await/of
ループで使用するasyncIteratorを返します。この関数を明示的に呼び出す必要はありません。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は、フラグなしでネイティブに非同期イテレータをサポートしています。--harmony_async_iteration
フラグを使用して、Node.js 8.xで非同期イテレータを有効にできます。
**注:** Symbol.asyncIterator
が未定義の場合、この関数は設定されません。 Symbol.asyncIterator
が未定義の場合、Node.jsバージョンが非同期イテレータをサポートしていないことを意味します。