1

I need to create 2000 documents at once in Meteor. I know I can use

for (i=0; i<2000; i++) {
    CollectionName.insert({});
}

but I hope there is a bulk create function in Meteor. How can I insert these 2000 rows in the fastest way possible?

Jamgreen
  • 10,329
  • 29
  • 113
  • 224
  • you way is correct. If you want a bulk then see http://stackoverflow.com/questions/19757434/bulk-mongodb-insert-in-meteor-or-node – Zeev G Jun 30 '15 at 06:50
  • Have a look at this: http://www.markdrew.co.uk/blog/post.cfm/importing-json-into-a-meteor-mongo-database – Billybobbonnet Jun 30 '15 at 07:29

3 Answers3

9

Meteor doesn't natively support this. However, it does give you access to the node Mongodb driver which can natively do a bulk insert.

You can only do this on the server:

var x = new Mongo.Collection("xxx");

x.rawCollection.insert([doc1, doc2, doc3...], function(err, result) {
    console.log(err, result)
});

Or with MongoDB 2.6 if your Meteor instance has access to it:

var bulk = x.initializeUnorderedBulkOp();

bulk.insert( { _id: 1, item: "abc123", status: "A", soldQty: 5000 } );
bulk.insert( { _id: 2, item: "abc456", status: "A", soldQty: 150 } );
bulk.insert( { _id: 3, item: "abc789", status: "P", soldQty: 0 } );
bulk.execute( { w: "majority", wtimeout: 5000 } );

Notes:

  • This is not synchronous or isn't run in a fiber as it uses the raw node driver. You need to use Meteor.bindEnvironment or Meteor.wrapAsync to create synchronous code
  • The documents are inserted unordered and may not be in the original order you added them in.
  • It may take 10 seconds for Meteor to see the documents 'Reactively' through a publish method if your instance is not oplog enabled.
Tarang
  • 75,157
  • 39
  • 215
  • 276
5

Extending @Akshat's answer, this is the syntax that would work on Meteor 1.0+

x = new Mongo.Collection("x");
var bulk = x.rawCollection().initializeUnorderedBulkOp();

bulk.insert( { _id: 1, item: "abc123", status: "A", soldQty: 5000 } );
bulk.insert( { _id: 2, item: "abc456", status: "A", soldQty: 150 } );
bulk.insert( { _id: 3, item: "abc789", status: "P", soldQty: 0 } );

Meteor.wrapAsync(bulk.execute)();
LPG
  • 822
  • 1
  • 11
  • 20
  • One thing that was tripping me up: if you have ObjectIDs that you're trying to insert, you need to convert them to Node's representation of ObjectID rather than Meteor's when inserting into a raw collection.`MongoInternals.NpmModule.ObjectID(your_obj_id._str);` – vroomfondel Feb 03 '17 at 19:39
  • 1
    For me, it threw an error then I have added the context as second parameter, so it worked. `Meteor.wrapAsync(bulk.execute, bulk)();` – Kishor Apr 20 '18 at 02:39
1

Here's what I use:

/server/fixtures.js

var insertIntoCollection = function(collection, dataArray){
  dataArray.forEach(function(item){
    collection.insert(item);
  });
};

if (Stuff.find().count() === 0) {

  var array = [
    { 
      // document1
    },{
      // document2
    }
  ];

  insertIntoCollection(Stuff, array);
};
fuzzybabybunny
  • 5,146
  • 6
  • 32
  • 58
  • Easy solution. Meteor 1.4 will have Mongo 3.2 with Bulk Insert. If you don't have 10,000 documents this solution will work very nicely. – TheBetterJORT Jun 09 '16 at 15:48