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

408
Vistas
Why is mongo dot notation replacing an entire subdocument?

I've got the following doc in my db:

{
  "_id": ObjectId("ABCDEFG12345"),
  "options" : {
    "foo": "bar",
    "another": "something"
  },
  "date" : {
    "created": 1234567890,
    "updated": 0
  }
}

And I want to update options.foo and date.updated at the same time using dot notation, like so:

var mongojs = require('mongojs');

var optionName = 'foo';
var optionValue = 'baz';

var updates = {};
updates['options.' + optionName] = optionValue;
updates['date.updated'] = new Date().getTime();

db.myCollection.findAndModify({
    query : {
        _id : ObjectId('ABCDEFG12345')
    },
    update : {
        $set : updates
    },
    upsert : false,
    new : true
}, function(error, doc, result) {

    console.log(doc.options);
    console.log(doc.date);

});

And this results in:

{
  foo : 'baz',
  another : 'something'
}
{
  updated : 1234567890
}

Specifically, my pre-existing date.created field is getting clobbered even though I'm using dot notation.

Why is this only partially working? The options sub-document retains its pre-existing data (options.another), why doesn't the date sub-document retain its pre-existing data?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The behavior described typically happens when the object passed in the $set operator is of the form { "data" : { "updated" : 1234567890 } } rather than { "data.updated" : 1234567890 }, but I'm not familiar with dots in JavaScript enough to tell if that could be the cause on JS's side.

Also, it wouldn't explain why it happens with data and not options.

If you could print the object stored in the variable updates and that is sent to MongoDB in the update field, that would allow to tell on which side the issue is (JS or MongoDB).

over 4 years ago · Santiago Trujillo Denunciar

0

i pass your code to a test environment and use the same library you are using. The mongojs library, for query by native ObjectId is like this mongojs.ObjectId("####") Can look the official documentation.

for the callback function in the findAndModify function, the docs parameter is an array so i navigate like an arrayenter image description here

Note: [to concatenate the string i use template literals] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)

All work fine...

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