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."
        }
    ],
}
6
  • what is your mongo version? Commented Mar 10, 2017 at 7:56
  • Possible duplicate of Update MongoDB field using value of another field Commented Mar 10, 2017 at 7:57
  • What have you already tried? Commented Mar 10, 2017 at 8: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); }); Commented Mar 10, 2017 at 8:52
  • @sidgate I want to update subarry elment from documents so the post refered by u is doing normal column update not array element. Commented Mar 10, 2017 at 9:32

2 Answers 2

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);
 });
Sign up to request clarification or add additional context in comments.

Comments

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);
});

1 Comment

yes its correct but I am implmented in more cleaner way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.