-
Type: Bug
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Shell API
-
None
-
5
mongosh exits when it encounters the end of its input and no more commands are running. However, this is an issue when Node.js builtin async APIs are used; for example,
cursor = db.getCollection("accounts").aggregate([{$match:{}}]) cursorAsStream = stream.Readable.from(cursor.map(entry => EJSON.stringify(entry) + '\n')); cursorAsStream.pipe(fs.createWriteStream('accounts'));
will truncate the resulting file when mongosh exits after the .pipe() call.
As a workaround, I suggested on slack to add while (!dest.writableFinished) sleep(10); in this scenario, but that’s obviously not something that we really want to recommend as the official solution to this issue.
We should think of a better way of solving this. It would be relatively easy to add a waitFor(promise) shell API, however, this would essentially mean committing to support for top-level await in mongosh scripts.