0

i want to update my all MongoDB doc who contain ObjectId in my nested array object

my doc is:

{
  "_id" : ObjectId("5a61a6bdae8bdc26685f8c27"), 
  "variant_count" : 1, 
  "salts" : [
    {
        "name" : "Cetirizine", 
        "classification" : "HD", 
        "dosage" : 0.0, 
        "is_cold_storage" : false, 
        "id" : ObjectId("5a61a292ae8bdc26685f2a91")
    }]
  "drug_category" : "DRUGS", 
  "name" : "Adicold Tab", 

}

i want to update all salts.id as a string,

for eg.

{
  "_id" : ObjectId("5a61a6bdae8bdc26685f8c27"), 
  "variant_count" : 1, 
  "salts" : [
    {
        "name" : "Cetirizine", 
        "classification" : "HD", 
        "dosage" : 0.0, 
        "is_cold_storage" : false, 
        "id" : "5a61a292ae8bdc26685f2a91"
    }]
  "drug_category" : "DRUGS", 
  "name" : "Adicold Tab", 
}
Devratna
  • 938
  • 1
  • 7
  • 26
  • You can change it as you will change any other field type. The below link will provide you the solution. Just use field name as salts.id. https://stackoverflow.com/questions/4973095/mongodb-how-to-change-the-type-of-a-field –  May 30 '18 at 18:26
  • Thanks @Novice it helps me to solve my issue, also posting update query for same, please review, if any other optimization required please let me know – Devratna May 30 '18 at 19:49

2 Answers2

0

this is works for me:

db.product.find({"salts.id" : {$type : 7}}).forEach(
    function(doc) {
    var salts = doc.salts;
    var newSalts = [];
    if (Array.isArray(salts) && salts.length > 0) {
      for (i = 0; i < salts.length; i++) { 
        salt = salts[i];
        if (salt['id'] != null && typeof salt['id'] == 'object') {
            salt['id'] = salt['id'].str
            newSalts.push(salt)
        } else {
          newSalts.push(salt)
        }
      } 
      db.product.update( { _id: doc._id }, { $set: { salts: newSalts } } 
     );
      i = i + 1;
    }
  }
);
Devratna
  • 938
  • 1
  • 7
  • 26
  • If this works for you, please carry on. I don't think any optimization is needed as this update will be run only once. It is not a recurring function. But yes, if you planning to run it in production DB and it was not a mandatory field, I would suggest you run some tests first, especially with variations of 'no such field in the collection', 'field assigned null rather than undefined ' etc.Is there really a reason to update it ? Coz most of the drivers provide a string representation of the Object ID, for your .net driver, its just an annotation over the variable. –  May 30 '18 at 20:48
  • Ah ok. Well, I will suggest you run some tests. Also, don't think you need the else part. If the id is present, change it to string and call update. Good luck !! –  May 30 '18 at 20:56
-1
**This code is working fine for me.**

db.collectionName.find({}).forEach(function(item){
                    if (item) {
                        item.salts.forEach(function(data){ 
                            if(data){
                                data.id = data.id.valueOf();
                            }
                        })
                        db.collectionName.save(item);
                    }
                })
Senthur Deva
  • 737
  • 4
  • 12