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

313
Visualizações
How to watch only the updated field of an object in Mongo change streams?

I have setup mongo streams for a product collection below but an update in any fields of the specs object returns the whole specs object instead of the updated field. I would expect a change of the display field to only return the display field rather than of the whole specs object product collection on mongo db

{
    "productName": "Apple iPhone 5",
    "specs": {
        "network": "GSM / CDMA / HSPA / EVDO / LTE",
        "display": "IPS LCD",
        "memory": "16GB 1GB RAM"
    }
}
     const productPipeline = [
         {
             $project: {
                 'fullDocument.productName': 1,
                 'updateDescription.updatedFields.specs': 1,
             },
         },
     ];
     const productChangeStream = this.productModel.watch(productPipeline, {
         fullDocument: 'updateLookup',
     });
     productChangeStream.on('change', async (data) => {
         console.log(JSON.stringify(data, null, 2));
     });    

I have tried to use the productPipeline below but still did not work

const productPipeline=[
            {
                $project: {
                    'fullDocument.productName': 1,
                    'updateDescription.updatedFields.specs.network': 1,
                    'updateDescription.updatedFields.specs.display': 1,
                    'updateDescription.updatedFields.specs.memory': 1,
                },
            },
        ];
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It looks behaviour is due to the difference between an update and a replace operation. MongoDB has two forms of update; a delta-update and a full-document replacement.

Replacements look like this:

db.coll.update({query for matching document}, {full replacement document})
OR
db.collection.replaceOne({query for matching document}, {full replacement document})

Normal (delta) updates look like this:

db.coll.update({query for matching document}, {$set: {individualField: newValue}})

If your application is performing full replacements, then the complete replacement document will be recorded in the oplog. Since change streams simply reports what it sees in the oplog, this will produce a change stream event with {operationType: "replace"} and a complete copy of the new document.

If, however, you do a normal update, then only the fields that were changed will be reported; you will receive a change stream event with {operationType: "update"} and a field called updateDescription which reports the fields that were modified by the operation. Please see this page for details of the update and replace events, and the updateDescription field.

For instance, if I insert the sample document you provided above and then run the following update:

db.testing.updateOne({}, {$set: {"branding.control_panel.companyLogo.displayName": "1200px-Logo_NIKE_updated.svg"}})

... then I receive the following change event (abbreviated for clarity):

{
    "_id" : ...,
    "operationType" : "update",
    "clusterTime" : ...,
    "ns" : ...,
    "documentKey" : ...,
    "updateDescription" : {
        "updatedFields" : {
            "branding.control_panel.companyLogo.displayName" : "1200px-Logo_NIKE_updated.svg"
        },
        "removedFields" : [ ]
    }
}

Here is the Jira issue

about 4 years ago · Juan Pablo Isaza 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