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

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

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?!

about 4 years ago · Santiago Trujillo
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!