Home > Blockchain >  MongoDB find with key that starts with a string pattern
MongoDB find with key that starts with a string pattern

Time:09-26

I'm trying to fetch a mongodb data using PHP. My Data is in the following format:

$document = $collection->findOne(
                     array('_id' => 'set1'), 
                     array('projection' => array('_id' => 1,'data'=>1))
            ); 
Result:

{
  "_id": "set1",
  "data": {
    "IND": {
      "2015-01": 0.6753404,
      "2015-02": 1.0502269,
      "2015-03": 1.0902269
    },
    "AUS": {
      "2015-01": 0.6753404,
      "2015-02": 1.0502269,
      "2015-03": 1.0902269
    },
    "IND_123": {
      "2015-01": 0.6753404,
      "2015-02": 1.0502269,
      "2015-03": 1.0902269
    }
  }
}

I am trying to fetch the output such that the result only has "IND" and "IND_123".

Is there a way to write the projection such that the result only provides "IND*" The following provides only "IND"

$document = $collection->findOne(
                         array('_id' => 'set1'), 
                         array('projection' => array('_id' => 1,'data.IND'=>1))
                ); 

CodePudding user response:

You can achieve it using an aggregation pipeline, like this:

db.collection.aggregate([
  {
    "$match": {
      "_id": "set1"
    }
  },
  {
    "$project": {
      data: {
        "$arrayToObject": {
          "$filter": {
            "input": {
              "$objectToArray": "$data"
            },
            "as": "item",
            "cond": {
              "$regexMatch": {
                "input": "$$item.k",
                "regex": "IND.*"
              }
            }
          }
        }
      }
    }
  }
])

Playground link. Here, we match the required document using $match. Then we, recompute the data field in $project, we first convert the data object to an array using $objectToArray, and filter all keys matching the pattern IND.* using $filter. Finally, we convert the filtered array back to an object using $arrayToObject.

  • Related