0

I have a collection that contains around 5000 documents. In each document there is field BrandID that I would like to change from string to MongoDB ObjectID. I have tried following shell command but it only updates first document. I dont get any error at all.

db.getCollection('SGProductRepository').find({ BrandID: {$ne : ""}}).forEach(function(obj) {    
    obj.BrandID = new ObjectId(obj.BrandID);
db.getCollection('SGProductRepository').save(obj);    
})

Any idea why it is not working. I am using RoboMongo as an editor. Thanks

felipsmartins
  • 13,269
  • 4
  • 48
  • 56
Fahad
  • 173
  • 1
  • 12
  • Well... It is so weird: `{ BrandID: {$eq : ""}}`. It means there many objects with empty BrandID? If so, empty string cannot be a valid object id (even though any errors is shown ) – felipsmartins Jul 04 '17 at 19:28
  • sorry I used not equal , just in the code snippet above as I was trying few thing I copied equal. Yes there are many blank values thats why I am using not equal. – Fahad Jul 05 '17 at 07:43

0 Answers0