3

I want to update the element of subarray with another element of same array index. Actually what mistake what I have done is typo mistake in column name (reviews, review) so want to update all reviews column with review. Can anybody give me the query to update this scenerio.

This is my document json

{
    "_id" : ObjectId("57a5df273c6c00d1378b456d"),   
    "review_time" : ISODate("2016-02-06T12:59:19.000Z"),
    "review" : "My name is Sandra P. and I am a satisfied customer. I've been working with eBrandz for a little more than two years and plan on keep doing so. They are always there whenever I have any concern and their approach is very professional. I would definitely recommend their services as I have seen great results.",
    "rating" : 5,
    "review_reply" : [ 
        {
            "author" : "http://new york.cylex usa.com/company/ebrandz inc 18441959.html",
            "reply_time" : "1969-12-31",
            "reviews" : "Sandra thanks for recommending ebrandz, appreciated."
        }, 
        {
            "author" : "http://new york.cylex usa.com/company/ebrandz inc 18441959.html",
            "reply_time" : "1969-12-31",
            "reviews" : "Sandra thanks for recommending ebrandz, appreciated.1"
        }, 
        {
            "author" : "http://new york.cylex usa.com/company/ebrandz inc 18441959.html",
            "reply_time" : "1969-12-31",
            "review" : "Sandra thanks for recommending ebrandz, appreciated22."
        }
    ],
}
Anant Waykar
  • 662
  • 2
  • 8
  • 18
  • what is your mongo version? – sidgate Mar 10 '17 at 07:56
  • Possible duplicate of [Update MongoDB field using value of another field](http://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field) – sidgate Mar 10 '17 at 07:57
  • What have you already tried? – Andriy Simonov Mar 10 '17 at 08:36
  • db.reviews.find({'review_reply.reviews' : {$exists : true}).forEach(function(doc) { doc.review_reply.review = doc.review_reply.reviews.length; db.collection.save(doc); }); – Anant Waykar Mar 10 '17 at 08:52
  • @sidgate I want to update subarry elment from documents so the post refered by u is doing normal column update not array element. – Anant Waykar Mar 10 '17 at 09:32
  • Answer provided below refers to same solution mentioned in the duplicate question. Obviously you need custom code in forEach as per your need – sidgate Mar 10 '17 at 09:58

2 Answers2

2
    db.temp_reviews.find( {'review_reply.reviews' : {$exists : true}  } ).forEach( function (doc) {
          doc.review_reply.forEach(function (z) {               
        if(z.reviews){        
           z.review =   z.reviews
           delete z.reviews;
        }
          });
          db.temp_reviews.save(doc);
 });
Anant Waykar
  • 662
  • 2
  • 8
  • 18
1

Your were close, you just need to loop through your array, check if wrong field exist, create the field, delete the wrong field :

db.reviews.find({
    'review_reply.reviews': {
        $exists: true
    }
}).forEach(function(doc) {
    for (var i = 0; i < doc.review_reply.length; i++) {
        if (doc.review_reply[i].hasOwnProperty("reviews")) {
            doc.review_reply[i].review = doc.review_reply[i].reviews;
            delete doc.review_reply[i].reviews;
        }
    }
    db.reviews.save(doc);
});
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159