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