Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

315
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda