I'm trying to create a command in Node JS using native mongodb driver, to remove the key value pair from an object which is inside the document object.
I have a mongoDB collection in the following format:
{
"name" : "PrakashPM"
"data" : {
"Jan-2017" : "2,3,1",
"Dec-2016" : "1,2,0",
"Nov-2016" : "9,9,9"
}
},
{
"name" : "Valavan"
"data" : {
"Jan-2017" : "1,1,1",
"Dec-2016" : "3,3,3",
"Nov-2016" : "9,9,9"
}
}
My target is to remove "Dec-2016" : "1,2,0" which is inside "name" : "PrakashPM"
My Code:
var mongoName = 'PrakashPM';
var mongoDate = "'data'.'Dec-2016'";
// TRIALS
// var mongoDate = "data.'Dec-2016'";
// var mongoDate = "data.Dec-2016";
var mongoVal = "'1,2,0'";
// TRIALS
// var mongoVal = "1,2,0";
mycollection.update( { name: mongoName },
{ $unset: {mongoDate : mongoVal} }
);
NOTE: I'm doing the above operations inside a PUT request function.
I tried many possible ways (TRIALS) for the input values (mongoDate, mongoVal) but I'm not able to achieve the result below.
Also, is it possible to remove the key value pair entry, just by using the key? (i.e. in this case {$unset: {mongoDate}} or something like that)
EXPECTED RESULT:
{
"name" : "PrakashPM"
"data" : {
"Jan-2017" : "2,3,1",
"Nov-2016" : "9,9,9"
}
},
{
"name" : "Valavan"
"data" : {
"Jan-2017" : "1,1,1",
"Dec-2016" : "3,3,3",
"Nov-2016" : "9,9,9"
}
}
req.bodycontent? Especially, what is the difference betweenreq.body.timerDataandreq.body.time. Also what istimerNamein your code?find( {name: mongoName}); the problem is with the $unset condition or may be additional query in the first argument.