title: MongoDB query to find property of first element of array
body: <p>I have the following data in MongoDB (simplified for what is necessary to my question).</p>
  
  <pre><code>{
      _id: 0,
      actions: [
          {
              type: "insert",
              data: "abc, quite possibly very very large"
          }
      ]
  }
  {
      _id: 1,
      actions: [
          {
              type: "update",
              data: "def"
          },{
              type: "delete",
              data: "ghi"
          }
      ]
  }
  </code></pre>
  
  <p>What I would like is to find the first action type for each document, e.g.</p>
  
  <pre><code>{_id:0, first_action_type:"insert"}
  {_id:1, first_action_type:"update"}
  </code></pre>
  
  <p>(It's fine if the data structured differently, but I need those values present, somehow.)</p>
  
  <p>EDIT: I've tried <code>db.collection.find({}, {'actions.action_type':1})</code>, but obviously that returns <em>all</em> elements of the actions array.</p>
  
  <p>NoSQL is quite new to me. Before, I would have stored all this in two tables in a relational database and done something like <code>SELECT id, (SELECT type FROM action WHERE document_id = d.id ORDER BY seq LIMIT 1) action_type FROM document d</code>.</p>