1

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"
             }
    }
3
  • 1
    Can you provide more sample of req.body content? Especially, what is the difference between req.body.timerData and req.body.time. Also what is timerName in your code? Commented Jan 24, 2017 at 10:45
  • Sorry for all the confusions. @SantanuBiswas I have edited the question with proper values. I'm able to retrieve the record using find( {name: mongoName}) ; the problem is with the $unset condition or may be additional query in the first argument. Commented Jan 24, 2017 at 11:07
  • See my reply below. Figured out your problem and provided solution. Commented Jan 24, 2017 at 11:10

3 Answers 3

1

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 })
Sign up to request clarification or add additional context in comments.

Comments

1

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 i.e. 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)

Comments

0

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} }
);

1 Comment

Sorry, my bad. I have edited my question in order to avoid confusions. I'm able to retrieve the record using find( {name: mongoName}) ; the problem is with the $unset condition. Even in your suggestion, instead of data, shouldn't it be 'data'.'Dec-2017' or data.Dec-2017? BTW, my suggestions didn't work as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.