[英]MongoDB Node JS - to delete an entry from an object inside a document object
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. 我正在尝试使用本机mongodb驱动程序在Node JS中创建命令,以从文档对象内部的对象中删除键值对。
I have a mongoDB collection in the following format: 我有以下格式的mongoDB集合:
{
"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" 我的目标是删除“名称”:“ PrakashPM”中的“ 2016年12月”:“ 1,2,0”
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. 注意:我正在PUT请求函数中执行上述操作。
I tried many possible ways (TRIALS) for the input values (mongoDate, mongoVal) but I'm not able to achieve the result below. 我为输入值(mongoDate,mongoVal)尝试了多种可能的方法(TRIALS),但无法实现以下结果。
Also, is it possible to remove the key value pair entry, just by using the key? 另外,是否可以仅通过使用键来删除键值对条目? (ie in this case {$unset: {mongoDate}} or something like that) (即本例中的{$ unset:{mongoDate}}或类似的东西)
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"
}
}
Assuming that req.body.timerDate has the month-date string value exactly as in MongoDB, this should work. 假设req.body.timerDate具有与MongoDB中完全相同的month-date字符串值,这应该可以工作。 (See documentation ). (请参阅文档 )。
You have to use string as key. 您必须使用字符串作为键。 You cannot use variable names there. 您不能在那里使用变量名。
// Assuming that req.body.timerDate
// has the month-date as stored in MongoDB (case-sensitive match)
var reqDate = "data." + req.body.timerDate;
var reqName = req.body.name;
var _unset = {};
_unset[reqDate] = "";
mycollection.update({ name: reqName }, { $unset: _unset })
Use the following example as a guide to updating your collection. 使用以下示例作为更新收藏集的指南。 You need to use the bracket notation to create your query and update documents ie you require an update operation which has the structure: 您需要使用方括号符号来创建查询和更新文档,即您需要具有以下结构的更新操作:
db.mycollection.update(
{ 'name': 'PrakashPM' },
{
'$unset': {
'data.Dec-2016': ''
}
}
)
So, using the variables to construct the objects to use in your operation 因此,使用变量来构造要在操作中使用的对象
var mongoName = 'PrakashPM';
var timerDate = 'Dec-2016';
var query = {};
var update = {'$unset': {}};
query['name'] = mongoName;
update['$unset']['data.'+timerDate] = '';
db.mycollection.update(query, update)
You are trying to use variables as keys in a object. 您正在尝试将变量用作对象中的键。
mycollection.update( { timerName: mongoName },
{ $unset: {mongoDate : mongoVal} }
);
This does not work as you expect and is a general JavaScript concept (not mongodb problem). 这不能按您预期的那样工作,并且是一个常规的JavaScript概念(不是mongodb问题)。 You are sending a query to mongo to update a row where the key "timerName" equals the content of the variable "mongoName". 您正在向mongo发送查询,以更新键“ timerName”等于变量“ mongoName”的内容的行。 But the proper key is "name". 但是正确的键是“名称”。
Try this: 尝试这个:
mycollection.update( { name: mongoName },
{ $unset: {data : mongoVal} }
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.