Creating a bulk write package, looking for feedback


Because I need to synchronize all my data with an external database (Youtube comments), I needed a way to perform a bulk upsert, in which I would pass in all resources in an response, find their ID id my database, and update them if anything has changed.

To do this i wrote the following as is looking for some feedback

var MongoClient = Meteor.npmRequire('mongodb').MongoClient;

Mongo.Collection.prototype.__mongoCollection = function(callback) {
  var self       = this;
  var connection = MongoInternals.defaultRemoteCollectionDriver().mongo;
  var url        = connection.db.options.url;

  MongoClient.connect(url, function(error, db) {
    if (error) { throw error; }

    var collectionName = self._name;
    var collection = db.collection(collectionName);
Mongo.Collection.prototype.upsertBulk = function(matcher, documents, options) {
  options = options || {};

  var operations = {
    _document._id =;

    var operation = {
      updateOne: {
        filter: {},
        update: { $set: _document },
        upsert: true
    operation['updateOne']['filter'][matcher] = _document[matcher];

    return operation;

  this.__mongoCollection(function(collection) {
    collection.bulkWrite(operations, options, function(error, result) {
      if (error) { throw error; }
      return result;


  • I open a new database connection on every function invocation, does Meteor keep track of the connection which I can use instead?
  • Is there a way to reduce the computation time from the map function?
  • Right now I use a lot of nested callbacks, any suggestions how to improve this?
  • Any general feedback?

Thanks, much appreciated