I use Mongoose and Faker to fill in the database with data for testing. There are two schemas - User and Job. I use loops for both schemas to create 100 users and 500 jobs. I want to disconnect from MongoDB when both loops finish their work as they run simultaneously and one will probably finish before another one but I don't know which one.
What if I use even more schemas, i.e. 10 * 200 entries for each running in different loops? How to manage this case principally?
use mongoose.createConnection() instead of mongoose.connect(). Each call to mongoose.createConnection() creates a new connection that is independent of any other open connections and can be closed without affecting the other connections. So just use a connection per loop.