1

In below mongo collection, how can i filter documents which contains key cat1 ?

{
    "_id" : ObjectId("xxxxxxxxxx"),
    "key1_cat1": "val1"
}
{
    "_id" : ObjectId("xxxxxxxxxx"),
    "key1_cat2": "val2"
}
{
    "_id" : ObjectId("xxxxxxxxxx"),
    "key2_cat1": "val3"
}
{
    "_id" : ObjectId("xxxxxxxxxx"),
    "key2_cat2": "val4"
}

I am looking for something like collection.find({"$key_contains":"cat1"}) which should filter doc1 and doc2 and output as

{
    "_id" : ObjectId("xxxxxxxxxx"),
    "key1_cat1": "val1"
}
{
    "_id" : ObjectId("xxxxxxxxxx"),
    "key2_cat1": "val3"
}
user2649233
  • 912
  • 4
  • 14
  • 28
  • A quick stackoverflow search gave me this result. Hope this helps [https://stackoverflow.com/questions/44078736/query-and-filter-key-names-instead-of-values-in-mongodb](https://stackoverflow.com/questions/44078736/query-and-filter-key-names-instead-of-values-in-mongodb) – code-mocker Jun 12 '18 at 04:28
  • sadly, the answers provided in above thread wasn't returning any data. – user2649233 Jun 12 '18 at 05:07

1 Answers1

1

I believe it cannot be done directly in Mongo. But we can use aggregation to achieve this with the usage of objectToArray

collection.aggregate([{
    '$project': {
        modifiedFormat: {
            $objectToArray: '$$ROOT'  // converts your object to array format [{k:, v:}, {k:, v:}]
        }
    }
}, {
    $match: {
        'modifiedFormat.k': {
            $regex: 'cat1' // your search on key(k)
        }
    }
}, {
    $project: {
        oldFormat: {
            $arrayToObject: '$modifiedFormat' // Making array to object again
        }
    }
}, {
    $replaceRoot: {
        newRoot: '$oldFormat' // replaceRoot to bring in the old format
    }
}])
RaR
  • 3,075
  • 3
  • 23
  • 48
  • there's an assertion failure in above query. `"errmsg" : "'newRoot' expression must evaluate to an object, but resulting value was: null. Type of resulting value: 'null'. Input document: {_id: 5b1f537f519b461d78e2f284, oldFormat: null}",` I tried it with the same collection mentioned in question. – user2649233 Jun 12 '18 at 05:06
  • @user2649233 There was a typo. It is fixed now – RaR Jun 12 '18 at 05:45
  • Thanks, that worked. But i have a situation where the key to find is inside an object. i.e `{"main_key":"key1_cat1": "val1"}` and now i have to filter based on subkey - how can the above be modified to achieve this requirement? something like `key contains main_key.cat1` should filter all relevant docs – user2649233 Jun 12 '18 at 10:09