Mongodb - watch changestream in documents with nested array element of certain value

596 Views Asked by At

Here is my document:

{
    "_id": {
        "$oid": "6257a55d04bf2167733f5b72"
    },
    "attributes": {
        "CustomerName": "John",
        "CustomerID": "28374",
        "LoanID": "82349327409234"
        "type": "Record",
        "Pointers": [
           {"type":"car","token_id":"123"},
           {"type":"house","token_id":"456"}
        ]
    }
}

Here is my watch query aiming to watch for Pointers elements with type:"car":

var watchCursor = db.loans.watch([
  {
    $match: {
      "$or": [
        {
          "updateDescription.updatedFields.attributes.Pointers": {
            $elemMatch: {
              "type": "car"
            }
          }
        },
        {
          "fullDocument.attributes.Pointers": {
            $elemMatch: {
              "type": "car"
            }
          }
        }
      ]
    }
  }
]);
while (!watchCursor.isExhausted()){
   if (watchCursor.hasNext()){
      print(JSON.stringify(watchCursor.next()));
   }
}

The problem is that I alter the document but it does not return any change results.

As a test, I changed the $elemMatch stage to $exists: true, then alter the document and it returned the changed document successfully.

what's wrong?!

0

There are 0 best solutions below