title: How to query an array of documents in MongoDB
body: <p>I have an array of objects like this one:</p>
  <pre><code>{
      &quot;actions&quot;: [{
          &quot;help&quot;: {
              &quot;messages&quot;: [{}, {}]
          }
      }, {
          &quot;animals&quot;: [{
              &quot;sea&quot;: {
                  &quot;messages&quot;: [{}, {}]
              }
          }, {
              &quot;land&quot;: {
                  &quot;messages&quot;: [{}, {}]
              }
          }]
      }]
  }
  </code></pre>
  <p>I'm trying to get the <code>messages</code> array from each element. (Only the matched one)</p>
  <p>I've tried something like:</p>
  <pre><code>db.getCollection('responses').find({&quot;actions&quot;: &quot;help&quot;})
  </code></pre>
  <p>And</p>
  <pre><code>db.getCollection('responses').find({&quot;actions.animals&quot;: &quot;sea&quot;})
  </code></pre>
  <p>With no luck as I'm still getting an empty array.</p>
  <p>If this isn't possible, I'm open for alternatives.</p>
  <p>I've been looking for similar questions like this one: <a href="https://stackoverflow.com/questions/11638224/mongo-db-querying-nested-array-and-objects">Mongo db - Querying nested array and objects</a> but in that question they're looking for a specific element inside the &quot;messages&quot; object (in my case) for example. Same as in this other question: <a href="https://stackoverflow.com/questions/17075205/query-for-a-field-in-an-object-in-array-with-mongo">Query for a field in an object in array with Mongo?</a> where they're using <code>$elementMatch</code> and I don't think it fits my needs.</p>
  <p>I thought that in the Mongo Tutorial: <a href="https://docs.mongodb.com/manual/tutorial/query-array-of-documents/" rel="nofollow noreferrer">Query array of documents</a> might help, but again, they have a similar structure like mine but they have a property for the <code>help</code> element for example, something like:</p>
  <pre><code>{
      &quot;actions&quot;: [{
          &quot;action&quot;: &quot;help&quot;,
          &quot;messages&quot;: [{}, {}]
      }, {
          &quot;action&quot;: &quot;animals&quot;,
          &quot;classifications&quot;: [{
              &quot;classification&quot;: &quot;sea&quot;,
              &quot;messages&quot;: [{}, {}]
          }, {
              &quot;classification&quot;: &quot;land&quot;,
              &quot;messages&quot;: [{}, {}]
          }]
      }]
  }
  </code></pre>
  <p>Is this the only way to make it to work? Or can I still maintain my original structure?</p>
  <h2>Edit</h2>
  <p>After trying @Graciano's answer I'm getting the following:</p>
  <pre><code>db.getCollection('responses').aggregate([{
      $project: { &quot;messages&quot;:  &quot;$actions.animals.sea.messages&quot; }
  }])
  </code></pre>
  <p>Result:</p>
  <pre><code>/* 1 */
  {
      &quot;_id&quot; : ObjectId(&quot;5ac42e65734d1d5beda1f99b&quot;),
      &quot;messages&quot; : [ 
          [ 
              [ 
                  {
                      &quot;type&quot; : 0,
                      &quot;speech&quot; : &quot;There are a lot of sea animals, to name a few: &quot;
                  }, 
                  {
                      &quot;type&quot; : 0,
                      &quot;speech&quot; : &quot;Whale\nFishes\nSharks&quot;
                  }
              ]
          ]
      ]
  }
  </code></pre>
  <p>Now the error to solve is that it must be a single array, not an array of arrays of arrays of messages... how to solve this?</p>