title: Mongodb query on nested array elements
body: <p>for my new project we started to use mongodb as a document repository. It works great for our requirements but now we need to implement a  more advanced query on sub-sub-array element.</p>
  
  <p>This is an example of our documents:</p>
  
  <pre><code>        {
              "Asset" : {
                  "Metadata" : {
                      "Titolo" : {
                          "Value" : "Titolo 50271235"
                      },
                      "Platforms" : {
                          "Platform" : [ 
                              {
                                  "@name" : "MY_PLATFORM_1",
                                  "PublishingRanges" : {
                                      "PublishingRange" : [
                                          {
                                              "@startdate" : ISODate("2013-09-05T00:00:00Z"),
                                              "@enddate" : ISODate("2013-11-04T23:59:00Z")
                                          },
                                          {
                                              "@startdate" : ISODate("2013-10-05T00:00:00Z"),
                                              "@enddate" : ISODate("2014-11-04T23:59:00Z")
                                          }
                                      ]
                                  }
                              }, 
                              {
                                  "@name" : "MY_PLATFORM_2",
  
                                  "PublishingRanges" : {
                                      "PublishingRange" : [
                                          {
                                              "@startdate" : ISODate("2013-09-05T00:00:00Z"),
                                              "@enddate" : ISODate("2013-11-04T23:59:00Z")
                                          },
                                          {
                                              "@startdate" : ISODate("2013-09-05T00:00:00Z"),
                                              "@enddate" : ISODate("2013-11-04T23:59:00Z")
                                          }
                                      ]
                                  }
                              }, 
                              {
                                  "@name" : "MY_PLATFORM_3",
                                  "AmbienteDiPubblicazione" : {
                                      "#" : "Produzione"
                                  },
                                  "PublishingRanges" : {
                                      "PublishingRange" : [
                                          {
                                              "@startdate" : ISODate("2013-09-05T00:00:00Z"),
                                              "@enddate" : ISODate("2013-11-04T23:59:00Z")
                                          },
                                          {
                                              "@startdate" : ISODate("2013-09-05T00:00:00Z"),
                                              "@enddate" : ISODate("2013-11-04T23:59:00Z")
                                          }
                                      ]
                                  }
                              }, 
                              {
                                  "@name" : "MY_PLATFORM_4",
                                   "PublishingRanges" : {
                                      "PublishingRange" : [
                                          {
                                              "@startdate" : ISODate("2013-09-05T00:00:00Z"),
                                              "@enddate" : ISODate("2013-11-04T23:59:00Z")
                                          },
                                          {
                                              "@startdate" : ISODate("2013-09-05T00:00:00Z"),
                                              "@enddate" : ISODate("2013-11-04T23:59:00Z")
                                          }
                                      ]
                                  }
                              }
                          ]
                          }
                      }
                  }
              }
          }
  </code></pre>
  
  <p>As you can see, we have an array of "Platform" which have inside an array of "PublishingRange".
  What we need is to find all the documents which the element "Platform" satisfy these constraints:</p>
  
  <pre><code>Platform.@name == VAR_PLATFORM
  and(
      (PublishingRange.@startdate &gt; VAR_START &amp;&amp; PublishingRange.@startdate &lt; V_END)
  )
  </code></pre>
  
  <p>Of course, the constraints on dates must be sastisfied by the "Platform" with name== VAR_PLATFORM, and not on a different "Platform"</p>
  
  <p>I've tried using an $elemMatch based query but without success.</p>
  
  <p>Thank you for any suggestion.</p>
  
  <p>Max</p>