0

My test collection has 56 entities in them. When the following script is executed the resulting collection has less entries than the original collection. The number varies for each run. What would cause this issue and is there a workaround for this?

var collectionToUpdate = 'testcollection';
var temporaryCollectionName = collectionToUpdate + '_old'

db.getCollection(collectionToUpdate).renameCollection(temporaryCollectionName);
var oldCollection = db.getCollection(temporaryCollectionName);

db.createCollection(collectionToUpdate);
var newCollection = db.getCollection(collectionToUpdate);

var count = 0;
oldCollection.find().forEach(
    function (element) {
        count++;
        newCollection.insert(element)
    }
);

print(count);

Versions used:

  • MongoDB - 3.2.8
  • RoboMongo - 0.9.0-RC10
Lian
  • 131
  • 1
  • 7
  • Why don't you use [`db.getCollection(collectionToUpdate).copyTo(temporaryCollectionName)`](https://docs.mongodb.com/manual/reference/method/db.collection.copyTo/)? – chridam Sep 23 '16 at 11:03
  • @chridam I'm actually updating one of the properties in the _id object and it seems that the only way to do that is to change the value and insert it back into the collection as the _id property is immutable. The above is a simplified version demonstrating the issue of missing documents from the forEach function. – Lian Sep 24 '16 at 15:16

0 Answers0