Mongodb changestream pipeline query does not work

108 Views Asked by At

My document is like this:

{
    "_id": {
        "$oid": "6257a55d04bf2167733f5b72"
    },
    "attributes": {
        "CustomerName": "John",
        "CustomerID": "28374",
        "LoanID": "82349327409234"
    },
    "type": "Record"
}

My changestream watch is like this:

var watchCursor = db.transactions.watch([ {$match: {"attributes.LoanID": { $exists : true }}} ]);
while (!watchCursor.isExhausted()){
   if (watchCursor.hasNext()){
      print(JSON.stringify(watchCursor.next()));
   }
}

But the change stream can't find this document. Any thoughts? Note that my search query is after the nested field inside attributes.

1

There are 1 best solutions below

0
On

Ah the reason is because the changestream event document is not an exact copy of the actual document.

In case of an update, the actual document resides in updateDescription.updatedFields and in case of an insert it resides in fullDocument field.

Ref: Mongodb change-events