我有一个来自 mongo 的流式查询,我将它传送到一个 through2“间谍”可写流。它完全包括带有 5 个文档的小集合的“结束”回调。但是,对于 344 个文档的更大集合,只有前 15 个通过,然后它永远挂起,并且“结束”事件永远不会触发。这是一个 MCVE:
var spy = require("through2-spy").obj;
var MongoClient = require("mongodb").MongoClient;
function getStream() {
var stream = spy(function() {
console.log("@bug counting", stream.total++);
});
stream.total = 0;
return stream;
}
function onEnd() {
console.log("ended");
}
MongoClient.connect(process.argv[2], function(error, db) {
if (error) {
console.error(error);
return;
}
var stream = db.collection(process.argv[3]).find().stream();
stream
// behavior is the same with the follow line commented out or not
.on("end", db.close.bind(db))
.on("error", console.error)
.on("end", onEnd)
.pipe(getStream());
});