Critical_Rebel
Critical_Rebel

Reputation: 39

How to check if a key exists in a mongodb object where the key is a value of some another field in the document while doing aggregation?

First of all I know we can check if a key exists using the dot operator but in my case it is not working and I dont know why.

So far in the aggregation pipeline I have the following records.

{
  "my_key":"1234"
  "data":{
    1234:"abc"
    4567:"xyz"
  }
}

{
  "my_key":"6666"
  "data":{
    1234:"abc"
    4567:"xyz"
  }
}

I want to return the document where the my_key value does not exists in the data object. So according to the above example it should return the 2nd document.

I was trying using the $match operator as following but it does not seem to work.

$match :
{
  "data.$my_key":{$exists:false}
}

This does not work and I dont get why :( Is it because the my_key value is a string and the keys in the data object are not strings?

Upvotes: 0

Views: 2866

Answers (1)

Gibbs
Gibbs

Reputation: 22964

playground

db.collection.aggregate([
  {
    "$project": {//Reshape the data
      "data": {
        "$objectToArray": "$data"
      },
      "my_key": 1
    }
  },
  {
    "$unwind": "$data"
  },
  {
    "$match": {//matching
      "$expr": {
        "$eq": [
          "$data.k",
          "$my_key"
        ]
      }
    }
  }
])

Another way

Wihtout unwind

db.collection.aggregate([
  {
    "$project": {
      "data": {
        "$objectToArray": "$data"
      },
      "my_key": 1
    }
  },
  {
    $project: {
      "output": {
        "$map": {
          "input": "$data",
          "as": "data",
          "in": {
            "$eq": [
              "$$data.k",
              "$my_key"
            ]
          }
        }
      },
      "data": 1,
      "my_key": 1
    }
  },
  {
    $match: {
      output: true
    }
  }
])

If you need original format of data, you can add the below as last stage

{
    $project: {
      "data": {
        "$arrayToObject": "$data"
      },
      "my_key": 1
    }
  }

Upvotes: 1

Related Questions