4

I scouted around to find the right solution for inserting a large amount of documents to MongoDB using Mongoose.

My current solution looks like this:

MongoClient.saveData = function(RecordModel, data, priority, SCHID, callback){
    var dataParsed = parseDataToFitSchema(data, priority, SCHID);
    console.log("Model created. Inserting in batches.");
    RecordModel.insertMany(dataParsed)
    .then(function(mongooseDocuments) {
         console.log("Insertion was successful.");
    })
    .catch(function(err) {
        callback("Error while inserting data to DB: "+err);
        return;
    })
    .done(function() {
       callback(null);
       return;
    });
}

But it appears to me there are other offered solutions out there. Like this one: http://www.unknownerror.org/opensource/Automattic/mongoose/q/stackoverflow/16726330/mongoose-mongodb-batch-insert

Using collection.insert. How is that different to the Model.insertMany?

Same goes for update, my previous question: What is the right approach to update many records in MongoDB using Mongoose

Asks how do I update big chunk of data with Mongoose, defined by _id. The answer suggests to use collection.bulkWrite while I am under impression Model.insertMany can do it too.

Community
  • 1
  • 1
Ondrej Tokar
  • 4,898
  • 8
  • 53
  • 103
  • 1
    You simply cannot do an update operation with `Model.insertMany` because you will be inserting new docs rather than updating existing onces. – chridam Aug 04 '16 at 08:30
  • And what is the difference between `Model.insertMany` and `collection.insert`? And why wouldn't they clarify it in official docs? – Ondrej Tokar Aug 04 '16 at 08:42
  • These posts may help you with clarification, [What's the difference between insert(), insertOne() and insertMany() method?](http://stackoverflow.com/questions/36792649/) and [MongoDB: Bulk insert (Bulk.insert) vs insert multiple](http://stackoverflow.com/questions/35112165/) – chridam Aug 04 '16 at 09:02

0 Answers0