Jamgreen
Jamgreen

Reputation: 11039

Insert thousands of docs in Meteor

What is the best way to insert thousands of rows into a collection in Meteor?

When I am inserting inside a loop, I can see on the page that the number of docs updates reactively due to Collection.find().count(). I am not interested in knowing the number of documents before the loop is over and all the docs are inserted. Are there any good practices to remember when inserting large amount of data in MongoDB?

Edit

When I use

createFolders: function() {
  var folders = [];

  for   (var i = 0; i < 10000; i++) {
    folders.push({ name: i });
  }

  Folders.batchInsert(folders);
},

the docs are inserted but the server says RangeError: Out of memory and restarts.

Upvotes: 0

Views: 268

Answers (3)

Karan R
Karan R

Reputation: 37

Meteor does not have a default functionality to do bulk inserts so your autorun will trigger every insert operation

This is a package that enables batch loads - mikowals:batch-insert

Upvotes: 1

bluebird
bluebird

Reputation: 640

you should be able to pass an array of documents to a single collection.insert() call

Mongo also provides some bulk insert operation. See here http://docs.mongodb.org/manual/core/bulk-write-operations/

Not sure if they are supported in Meteor though.

Upvotes: 0

TheHuman Wall
TheHuman Wall

Reputation: 207

If you're not interested in getting everything published to the database all the time for every user, you can just remove the autopublish package from your project,meteor remove autopublish, if you haven't already. This way, you can restrict the meteor magic from happening until after you have inserted all of the rows. When you are done inserting, you can use meteor.publish on the entire collection to 'push' it to all of the users.

Not entirely sure if this was what you were thinking, hope it helps

Upvotes: 0

Related Questions