title: Regex inside array in mongoDB
body: <p>i want to do a query inside a array in mongodb with regex, the collections have documents like this:</p>
  
  <pre><code>{
  "_id" : ObjectId("53340d07d6429d27e1284c77"),
  "company" : "New Company",
  "worktypes" : [ 
      {
          "name" : "Pompas",
          "works" : [ 
              {
                  "name" : "name 2",
                  "code" : "A00011",
                  "price" : "22,22"
              }, 
              {
                  "name" : "name 3",
                  "code" : "A00011",
                  "price" : "22,22"
              }, 
              {
                  "name" : "name 4",
                  "code" : "A00011",
                  "price" : "22,22"
              }, 
              {
                  "code" : "asdasd",
                  "name" : "asdads",
                  "price" : "22"
              }, 
              {
                  "code" : "yy",
                  "name" : "yy",
                  "price" : "11"
              }
          ]
      }, 
      {
          "name" : "name 4",
          "works" : [ 
              {
                  "code" : "A112",
                  "name" : "Nombre",
                  "price" : "11,2"
              }
          ]
      },          
      {
          "name" : "ee",
          works":[
  
              {
                  "code" : "aa",
                  "name" : "aa",
                  "price" : "11"
              }, 
              {
                  "code" : "A00112",
                  "name" : "Nombre",
                  "price" : "12,22"
              }
                ]
      }
  ]
  </code></pre>
  
  <p>}</p>
  
  <p>Then i need to find a document by the company name and any work inside it have match a regex in code or name work. 
  I have this:</p>
  
  <pre><code>var companyquery = { "company": "New Company"};
  var regQuery = new RegExp('^A0011.*$', 'i');
  
  db.categories.find({$and: [companyquery,
              {$or: [
                  {"worktypes.works.$.name": regQuery},
                  {"worktypes.works.$.code": regQuery}
              ]}]})
  </code></pre>
  
  <p>But dont return any result..I think the error is try to search inside array with de dot and $..
  Any idea?</p>
  
  <p>Edit:</p>
  
  <p>With this:</p>
  
  <pre><code>db.categories.find({$and: [{"company":"New Company"},
              {$or: [
                  {"worktypes.works.name": {"$regex": "^A00011$|^a00011$"}},
                  {"worktypes.works.code": {"$regex": "^A00011$|^a00011$"}}
              ]}]})
  </code></pre>
  
  <p>This is the result:</p>
  
  <pre><code>{
      "_id" : ObjectId("53340d07d6429d27e1284c77"),
      "company" : "New Company",
      "worktypes" : [ 
          {
              "name" : "Pompas",
              "works" : [ 
                  {
                      "name" : "name 2",
                      "code" : "A00011",
                      "price" : "22,22"
                  }, 
                  {
                      "code" : "aa",
                      "name" : "aa",
                      "price" : "11"
                  }, 
                  {
                      "code" : "A00112",
                      "name" : "Nombre",
                      "price" : "12,22"
                  }, 
                  {
                      "code" : "asdasd",
                      "name" : "asdads",
                      "price" : "22"
                  }, 
                  {
                      "code" : "yy",
                      "name" : "yy",
                      "price" : "11"
                  }
              ]
          }, 
          {
              "name" : "name 4",
              "works" : [ 
                  {
                      "code" : "A112",
                      "name" : "Nombre",
                      "price" : "11,2"
                  }
              ]
          }, 
          {
              "name" : "Bombillos"
          }, 
          {
              "name" : "Pompas"
          }, 
          {
              "name" : "Bombillos 2"
          }, 
          {
              "name" : "Other type"
          }, 
          {
              "name" : "Other new type"
          }
      ]
  }
  </code></pre>
  
  <p>The regex dont field the results ok..</p>