How to listen for changes in embedded documents in mongodb

I have a collection teams with sample documents:

{
  "_id": {
    "teamName": "BILALATI6449",
    "tournamentId": "197831"
  },
  "tournamentName": "Amanora Cricket Championship 2021",
  "player1": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "player2": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "player3": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "player4": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "player5": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "player6": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "player7": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "player8": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "player9": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "player10": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "player11": {
    "$oid": "6187d65514f9a67bcf9e8102"
  },
  "__v": 0
}

All the players object have embedded documents from pointotals. How can I listen for changes in these documents. I tried this code but id doesn’t work.

const collection = client.db("mysquad11").collection("pointtotals");

const changeStream = collection.watch();

changeStream.on("change", (next) => {
    if (next.operationType === "update") {
        console.log(next);
    }
});

Can someone help me to tackle the problem? I, only want to listen to the changes of embedded documents, not all documents of pointTotals collection.

1 Like