skip to Main Content

enter image description 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

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. You can do it with Aggregation Framework:

    • $match – to filter documents based on some custom criteria
    • $eq and $toString – To check it _id and history_doc.data.objectId are the same.
    db.collection.aggregate([
      {
        "$match": {
          "$expr": {
            "$eq": [
              {
                "$toString": "$_id"
              },
              "$history_doc.data.objectId"
            ]
          }
        }
      }
    ])
    

    Working example

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search