sonu kumar
sonu kumar

Reputation: 47

how to $set Data in mongodb update

Below is the data fields i have in my collection.

  { "_id" : "TLa7L9HJTabD6ooLJ", "userId" : "dgS4gJtoEPoRrGE2b", "data" : { "contact" : [ "firstname", "lastname", "phone", "email", "leadsource" ] } }

i am try to replace data.contact array value with new array i am using this query

setting.update({
                _id: doc.userId
            }, {
                $set: {
                    "data.contact":["data1","data2"]
                }
            },function(error){
                if(error){
                    console.log(error.reason);
                }else{
                    toastr.success('User Details updated.');
                }
            });

Once my above query run i got success message but in Database this value

"data.contact":["data1","data2"] still not updated.

Upvotes: 0

Views: 78

Answers (1)

Bonanza
Bonanza

Reputation: 1621

Are you sure you are not mistaken in this line:

{
    _id: doc.userId
}

shouldn't it be:

{
    userId: doc.userId
}

because you have such field in database. Maybe it's just a simple mistake.

Upvotes: 1

Related Questions