There is a js script in which there is a connection with mongodb, after which a lot of various updates are made to the database, and some more asynchronous operations. The script runs on the crown. The problem is that the processes will accumulate in the memory, because I do not know at what point to break the connection with the database. Tried to wrap in promise:
mongoose.connect( 'mongodb://' + config.DB_HOST + '/' + config.DB_NAME ); var promise = new Promise( function( resolve, reject ) { // много запросов асинхронных сложных resolve(); }); promise.then( function() { mongoose.connection.close(); }); A connection is broken faster than updates are made, and all business logic crashes. There was an idea with setTimeout, but this is a terrible crutch. What else is there to catch the request identifiers in db.currentOp (). Inprog, to memorize them and wait for everyone to finish? It is quite difficult somehow. How can I wait for the completion of the work of the Mongus, all his requests? Maybe he has some property? I can not find information anywhere.
UPDATE
Tried through stream - also to no avail:
var stream = userModel.find( условия ).stream(); stream.on('data', function ( data ) { // много запросов асинхронных сложных }).on('close', function () { console.log( 'connection closed..' ); mongoose.connection.close(); }); However, it “leaks” more updates than in the first case. But the connection is still closed, without waiting for the completion of the remaining updates.