Bulk upsert in MongoDB using mongoose

Not in “mongoose” specifically, or at least not yet as of writing. The MongoDB shell as of the 2.6 release actually uses the “Bulk operations API” “under the hood” as it were for all of the general helper methods. In it’s implementation, it tries to do this first, and if an older version server is detected then there is a “fallback” to the legacy implementation.

All of the mongoose methods “currently” use the “legacy” implementation or the write concern response and the basic legacy methods. But there is a .collection accessor from any given mongoose model that essentially accesses the “collection object” from the underlying “node native driver” on which mongoose is implemented itself:

 var mongoose = require('mongoose'),
     Schema = mongoose.Schema;

 mongoose.connect('mongodb://localhost/test');

 var sampleSchema  = new Schema({},{ "strict": false });

 var Sample = mongoose.model( "Sample", sampleSchema, "sample" );

 mongoose.connection.on("open", function(err,conn) { 

    var bulk = Sample.collection.initializeOrderedBulkOp();
    var counter = 0;

    // representing a long loop
    for ( var x = 0; x < 100000; x++ ) {

        bulk.find(/* some search */).upsert().updateOne(
            /* update conditions */
        });
        counter++;

        if ( counter % 1000 == 0 )
            bulk.execute(function(err,result) {             
                bulk = Sample.collection.initializeOrderedBulkOp();
            });
    }

    if ( counter % 1000 != 0 )
        bulk.execute(function(err,result) {
           // maybe do something with result
        });

 });

The main catch there being that “mongoose methods” are actually aware that a connection may not actually be made yet and “queue” until this is complete. The native driver you are “digging into” does not make this distinction.

So you really have to be aware that the connection is established in some way or form. But you can use the native driver methods as long as you are careful with what you are doing.

Leave a Comment