Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

Mongoose 4.4 now has an insertMany function which lets you validate an array of documents and insert them if valid all with one operation, rather than one for each document:

var arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];

Movies.insertMany(arr, function(error, docs) {});

If I have a very large array, should I batch these? Or is there no limit on the size of the array?

For example, I want to create a new document for every Movie, and I have 10,000 movies.

1 Answer

0 votes
by (108k points)

insertMany group of processes can have at most 1000 operations. If a group surpasses this limit, MongoDB will divide the group into smaller groups of 1000 or less. For example, if the queue contains 2000 operations, MongoDB creates 2 groups, each with 1000 operations.

The sizes and grouping workings are internal performance details and are subject to change in future versions. Executing an ordered list of operations on a sharded collection will usually be slower than executing an unordered list since with an ordered list, each operation must wait for the previous operation to finish.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...