Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

315
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!