Reputation: 14581
I'm creating some fake documents by 'expanding' the existing documents by 20 times. But; the forEach loop never seems to end. Why?
db['COLLECTION'].find({}).forEach(function(doc){
for( var x = 0; x < 20; x++ ) {
delete doc['_id'];
doc['Author'] = randArrayElement(names); /* chooses random name */
doc['Description'] = buzzword(); /* makes something up*/
db['COLLECTION'].insert(doc);
}
}
)
Upvotes: 3
Views: 2813
Reputation: 222531
I think you need to use snapshot here:
db['COLLECTION'].find().snapshot().forEach(function(doc){
...
})
I assume that this happens because write operations might result in a move of the document and snapshot fixes it:
The $snapshot operator prevents the cursor from returning a document more than once because an intervening write operation results in a move of the document.
If this will not help, then I have another idea that the newly created documents are picked up by the cursor. To overcome this I would create an array of all the documents, and then separately iterate them and do your 20 inserts for each one.
Upvotes: 5