0

I want to update specific field in my document. can someone please help me on this

 var q = {
            _id: mongoskin.helper.toObjectID(req.params._id)
        };
     db.collection.findOne(q., function(err, result) {
                    if (err) return next(err);
                 if (!result) {
                    return res.status(400).send({
                        error: "data not found"
                    });
                } else {
                       var fields = req.body;
                        db.collection.update(q,
                        {
                        $set: fields
                    }, function(err, result) {
                            console.log("err", err)
                            if (err) return next(err);
                            return res.send({
                                status: "success",
                                data: result
                            });
                        });
                    }

                })

Data in db is like this

[ {
            "_id": "59b631cd709fef7c4caf5dee",
            "name": "ddfdd",
            "home": {
                "a1": "f",
                "a2": "cc",
                "a3": "ccc",
                "a4": "gfr" 
            },
            "about": {
                "a5": "fgfffgf",
                "a6": "fgfg",
                "a7": "werr"
},contact:{
          a8:"hfjdfh"
          a9:"hyyd"
     }
},{
            "_id": "4546565756756",
            "name": "dde",
            "home": {
                "a1": "ff",
                "a2": "cfrc",
                "a3": "ccdec",
                "a4": "gfetr" 
            },
            "about": {
                "a5": "feeeffgf",
                "a6": "tyu",
                "a7": "ert"
},contact:{
          a8:"frt"
          a9:"rty"
     }
},.....]

this is just an example i have more than 250 fields in json data, i tried using split(',') it works but i dont need lengthy code/line.

i have updated my db data please check

Schüler
  • 512
  • 4
  • 10
  • 25
  • It's not JSON and it's not a string. The sooner you understand that concept the better. Native data structure to the language you are using is all you really need to know. You update each property by using ["dot notation"](https://docs.mongodb.com/manual/core/document/#dot-notation) to represent the path. – Neil Lunn Sep 11 '17 at 11:06
  • @NeilLunn if i have only `home` and `about` i can use this `db.collection.update( { _id:...} , { $set: { some_key.param2 : new_info } } ` but as i said above i have more fields `home:{},about:{},contact:{},Policies:{}.... etc` – Schüler Sep 11 '17 at 11:31

0 Answers0