0

I wish to fill a collection with user accounts, full of dummy data ofc. Now I don't expect calling 1 mil inserts will be smart, so I require how to do it database-side.

I sort of remember something about doing javascript in the database and perhaps that would be the way, but more specifically?

Discipol
  • 3,137
  • 4
  • 22
  • 41
  • You could break them into batches of 16meg and use batch insert, I think in node.js you can just send multiple documents into insert() – Sammaye Sep 05 '13 at 11:29
  • do you mean build an array with the documents, a 1 mil sized array and do one insert? I am frightened, yet curious. I shall try it after work :D I think the document size is 16 meg max, not the batch insert, right? – Discipol Sep 05 '13 at 11:44
  • 1
    All communication to the database is in the form of BSON documents, the query document you send down for exmaple is a BSON document, the parameter that goes into batchInsert is a BSON document representing an array of BSON documents – Sammaye Sep 05 '13 at 11:50
  • Ah, I understand. I'll give it a go later on and report back. – Discipol Sep 05 '13 at 11:59

1 Answers1

1

While you could just write code to insert documents using your favorite programming language (as many of the drivers offer techniques to insert documents in batches via an array structure), I'd suggest you consider creating either a JSON file or a CSV file containing the structure of your documents (maybe in multiple files if necessary for the import to work), and then using mongoimport, import all of the data.

http://docs.mongodb.org/manual/reference/program/mongoimport/

This way, you can create the file(s) once, and run the import directly from the database server without installing extra software/platforms/node/etc.

If you wanted to use Node.JS, you can use insert (documentation) and simply pass in an array of documents. I'd suggest doing it batches of some size far less than one million, and since it's going to be write heavy anyway, just do one batch at a time (don't rely on async behavior of JavaScript to get many connections going).

WiredPrairie
  • 58,954
  • 17
  • 116
  • 143