简体   繁体   中英

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.

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? (ie 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"
             }
    }

Assuming that req.body.timerDate has the month-date string value exactly as in MongoDB, this should work. (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). You are sending a query to mongo to update a row where the key "timerName" equals the content of the variable "mongoName". But the proper key is "name".

Try this:

mycollection.update( { name: mongoName },
{ $unset: {data : mongoVal} }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM