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

407
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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