Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

314
Visualizações
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? (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"
             }
    }
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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 })
over 4 years ago · Santiago Trujillo Relatório

0

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)
over 4 years ago · Santiago Trujillo Relatório

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} }
);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda