0

I'm trying to bulk insert thousands of entries into Mongo, what I'm looking for is some sort of way to define a template and only change 1 / multiple variables in that document instead of sending thousands of huge documents that are actually the same thing, only having something like a number changed in them.

For example, a simple example that works in the Mongo Shell would be:

var myUniqueNumbers = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233];
var col = db.getCollection('userSiteTimes');

myUniqueNumbers.forEach(function(i){
   col.insert({hugeDocument: true, x: i}); 
});

How can I do something similar via the Node.js Driver?

Eek
  • 1,740
  • 1
  • 15
  • 24
  • just do exactly the same like you did here, what is the problem?... anyway, I suggest to use `insertMany` to insert bulks of documents – TomG Mar 15 '16 at 16:57
  • I want to do it via Node.js, specifically via `node-mongodb-native` – Eek Mar 16 '16 at 10:08
  • I don't want o use `insertMany`, because that will mean I will have to send each document in an array, let's consider my document has 1MB, and I need to insert 4k documents, and the only difference in them is a number, I want to send a template, that has 1MB and the 4k different numbers that are super small in size. In Shell I would accomplish that via the example above, but how about via Node? – Eek Mar 16 '16 at 10:10
  • Exactly like you did here, I don't understand why you think there is a difference. https://github.com/mongodb/node-mongodb-native – TomG Mar 16 '16 at 10:15
  • If I do that via Node, node will create a variable with my Unique Number, and node will pass through each of the number and for each of the numbers it will issue a insert command for Mongo. That means my node process will hang until it completes. I want a way to send the whole snippet to Mongo and Mongo to handle it :) Sort of like a function – Eek Mar 16 '16 at 10:43
  • So do it in non blocking function or insertMany with multiple chunks, or send it to a promise function, there are lots of ways you can do it. – TomG Mar 16 '16 at 11:58
  • Yes, I know there are a lot of ways to do it, I was just trying to see if there is a better way to do it, with something like a procedure in SQL... – Eek Mar 16 '16 at 16:25

0 Answers0