Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MongoDB filter when two fields equal

my data here

Does anyone who knows how to filter data and only keep those history_doc.data.objectId equals _id?

I have tried so many methods but none of them works

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'history_doc.data.objectId': {$eq: '$_id'}}
{'history_doc.data.objectId': {$eq: {$toString: '$_id'}}}

>Solution :

You can use $expr and $eq with $toObjectId into aggregation query like this:

.aggregate({
  $match: {
    $expr: {
      $eq: [
        "$_id",
        {
          "$toObjectId": "$history_doc.data.objectId"
        }
      ]
    }
  }
})

Example here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading