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

202
Views
How to listen specific fields in mongodb database by changestream?

I have a collection on my mongodb database name users and every user contains a field name posts . I want to get realtime changes data of that posts field when a user add a new post or update existing post and take the updated value from socket server and send it back to frontend.

a single user documents looks like this

 [  
    {
        "_id" : "613fb31a52ef63541d036fcd",
        "posts"["613fb31a52ef63541d036fdf", "613fb31a52ef63541d036ff1"],
        "mobiles" : [ "01751150412"],
        "languages" : [ "English", "Bangla"],
     }
]

At first, I tried with const userChangeStream = connection.collection("users").watch()

that gives me all the fields of the user collection that wasn't even changed.

Then i followed this stackoverflow question and updated my code

 const filter = 
 [
   {
      $match: 
      {
        $and: 
        [
          { "updateDescription.updatedFields.posts": { $exists: true } },
          { operationType: "update" }
        ]
      }
   }
]

const options = { fullDocument: 'updateLookup' };

const postsChangeStream =    connection.collection("users").watch(filter,options);

postsChangeStream.on("change", (change) => {
  console.log('change on', change);
});

Now I dont get any response when adding on that posts field .

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