title: REGEX Searching in pymongo
body: <p>I am attempting to create a search in pymongo using REGEX. After the match, I want the data to be appended to a list in the module. I thought that I had everything set, but no matter what I set for the REGEX it returns 0 results. The code is below:</p>
  
  <pre><code>REGEX = '.*\.com'
  
  def myModule(self, data)
      #after importing everything and setting up the collection function in the DB I call the following:
      cursor = collection.find({'multiple.layers.of.data' : REGEX})
      data = []
      for x in cursor:
          matches.append(x)
      return matches
  </code></pre>
  
  <p>This is but one module of three I am using to filter through a huge amount of json files that have been stored in a mongodb. However, no matter how many times I change this formatting such as /.*.com/ to declare in the operation or using the $regex in mongo...it never finds my data and appends it in the list. </p>
  
  <p>EDIT: Adding in the full code along with what I am trying to identify:</p>
  
  <pre><code>RegEx = '.*\.com' #Or RegEx = re.compile('.*\.com')
  
  def filterData(self, data):
         db = self.client[self.dbName]
         collection = db[self.collectionName]
         cursor = collection.find({'data.item11.sub.level3': {'$regex': RegEx}})
         data = []
         for x in cursor:
             data.append(x)
         return data
  </code></pre>
  
  <p>I am attempting to parse through JSON data in a mongodb. The data is structured like so:</p>
  
  <pre><code>"data": {
      "0": {
          "item1": "something",
          "item2": 0,
          "item3": 000,
          "item4": 000000000,
          "item5": 000000000,
          "item6": "0000",
          "item7": 00,
          "item8": "0000",
          "item9": 00,
          "item10": "useful",
          "item11": {
              "0000": {
                  "sub": {
                      "level": "letter",
                      "level1": 0000,
                      "level2": 0000000000,
                      "level3": "domain.com"
                  },
                  "more_data": "words"
              }
          }
      }
  </code></pre>
  
  <p>UPDATE: After further testing it appears as though I need to include all of the layers in the search. Thus, it should look like </p>
  
  <p><code>collection.find({'data.0.item11.0000.sub.level3': {'$regex': RegEx}})</code>. </p>
  
  <p>However, the "0" can be 1 - 50 and the "0000" is randomly generated. Is there a way to set these to index's as variables so that it will step into it no matter what the value? It will always be a number value. </p>