'use strict' const MongoClient = require('mongodb').MongoClient const url = 'mongodb://localhost:27017/test'; var count = 0 var max = 1000 MongoClient.connect(url, function(err, db) { if (err) { throw err } const collection = db.collection('data') function insert (err) { if (err) throw err if (count++ === max) { return db.close() } collection.insert({ value: Math.random() * 1000000 }, insert) } insert() })