Pika
Pika

Reputation: 154

mongodb updating only specific subelements

my db structure looks like:

{
  "_id" : ObjectId("51e66873f6a6600436000001")
  ,"email" : "[email protected]",
  ,"attribute_group_a" : {
     "attribute_a" : ""
     ,"attribute_b" : ""
     ,"attribute_c" : ""
     ,"attribute_d" : ""
  },
  ,"attribute_group_b" : {
      "attribute_subgroup_b_a" : {
           "attribute_a" : ""
           ,"attribute_b" : ""
           ,"attribute_c" : ""
           ,"attribute_d" : ""
       }
       ,"attribute_subgroup_b_b" : { 
           "attribute_a" : ""
           ,"attribute_b" : ""
           ,"attribute_c" : ""
           ,"attribute_d" : ""
       }
  }
}

so lets say i want to update att_subgrp_b_a:

exports.updateProfil = function(req, res, item, callback) {
    var email = req.session.email;
    db.collection('profiles', function(err, collection) {
        collection.update({"email": email},{$set: item}, function(err, result)

the var "item" looks like:

{
    attribute_group_b:
    {
        attribute_subgroupgroup_b_a: 
        {
            att_a: "xy"
            ,att_b: "xy"
        }
    }
}

when i now update the file => it deletes everything in attr_group_b and replaces it with "item"

that means attr_subgrp_b_b is totally gone and all other attributes (of attr_subgrp_b_a) that weren't updated




i want that it looks for the attributes in "item", replaces them into db letting all other obj untouched

Upvotes: 1

Views: 62

Answers (1)

sachin
sachin

Reputation: 14365

Try the query below

  var email='emailid';
  var item='whichuwanttoupdate';
  collection.update(
             {"email": email},
             {$set:{'attribute_group_b.attribute_subgroup_b_a':item}},
             function(err,result){

     });

Upvotes: 1

Related Questions