title: How to implement MongoDB nested $elemMatch Query in C#
body: <p>I have a MongoDB collection in the following format.</p>
  
  <pre><code>{
  "_id" : ObjectId("56c6f03ffd07dc1de805e84f"),
  "Details" : {
      "a" : [
              [ { 
                  "DeviceID" : "log0", 
                  "DeviceName" : "Dev0"
                },
                { 
                  "DeviceID" : "log1", 
                  "DeviceName" : "Dev1"
                }
              ],
              [ { 
                  "DeviceID" : "Model0", 
                  "DeviceName" : "ModelName0"
                },
                { 
                  "DeviceID" : "Model1", 
                  "DeviceName" : "ModelName1"
                }
              ]
          ]
      }
  }
  </code></pre>
  
  <p>And I am trying to fetch all the documents where the <code>DeviceName</code> in array "a" contains a particular value, say "Name0". However I could get the desired result while using below Mongo query:</p>
  
  <pre><code>db.test_collection.find({"Details.a":{$elemMatch:{$elemMatch:{DeviceName : /.*Name0.*/}}}});
  </code></pre>
  
  <p>Now I am struggling to implement the above query in C#. Can anyone guide me with that?</p>
  
  <p>so far I have tried the below code and it was not working as expected</p>
  
  <pre><code>query = Query.And(Query.ElemMatch("Details.a", Query.And(Query.ElemMatch("DeviceName", Query.Matches("DeviceName", new BsonRegularExpression("Name0"))))));
  </code></pre>
  
  <p>Thanks in advance</p>