4

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 .

Nurul Islam
  • 151
  • 6

0 Answers0