title: Conditionally evaluate an array element to return
body: <p>Considering the following data, I want to return the results of what id the <strong>default</strong> document to choose from within an Array field in MongoDB. Let's call the collection <code>books</code>. A sample collection data is shown below:</p>
  
  <pre><code>[
    {
       name: "Book1",
       refs: [{ oid: "object1" }, { oid: "object2" }, {oid: "object5", default: true }]
    },
    {
      name: "Book2",
      refs: [{ oid: "object3" }, { oid: "object5", default: true }, { oid: "object7" }]
   },
   {
      name: "Book3",
      refs: [{ oid: "object4" }, { oid: "object2" }]
   },
   {
     name: "Book4",
     refs: [{ oid: "object5" }, { oid: "object4", default: true } ]
   }
  ]
  </code></pre>
  
  <p>Okay. So a lot of the key values are in there for brevity but this doesn't change the point.</p>
  
  <p>The desired logic here is as follows:</p>
  
  <ol>
  <li>Find and return the document in the <code>refs</code> Array field that has a default of <strong>true</strong></li>
  <li>If there is no matching document in the array return the first document in the array</li>
  </ol>
  
  <p>And following that logic, I would really like to see something returned as follows :</p>
  
  <pre><code>[
    {
      name: "Book1"
      refs: [{oid: "object5", default: true }]
    },
    {
      name: "Book2",
      refs: [{ oid: "object5", default: true }]
    },
    {
      name: "Book3",
      refs: [{ oid: "object4" }]
    },
    {
      name: "Book4",
      refs: [{ oid: "object4" }]
    }
  ]
  </code></pre>
  
  <p>Now I know there is the <a href="http://docs.mongodb.org/manual/reference/operator/aggregation/cond/" rel="nofollow noreferrer">$cond</a> operator in the aggregation pipeline, but part of this condition seems to be bound to getting a <a href="http://docs.mongodb.org/manual/reference/operator/projection/slice/" rel="nofollow noreferrer">$slice</a> on the projection where the <code>default</code> property does not exist on the document ( and is probably set to true but exists should suffice ).</p>
  
  <p>This logic pattern rests on the expected results of using <a href="http://docs.mongodb.org/manual/reference/operator/update/pull/" rel="nofollow noreferrer">$pull</a> to remove the element matching:</p>
  
  <pre><code>oid: "object5"
  </code></pre>
  
  <p>Out of each document array and then still be able to <strong>fall back</strong> to the first element of the array in a query</p>
  
  <p>So I'm looking for some <strong>strong</strong> <code>fu</code> to be able to return the results.</p>
  
  <p>And the <strong>solution</strong> cannot be to add another field in the main document referencing the value of the <strong>default</strong> field in the array document. Not having this is actually the point so the <code>$pull</code> operation works in a <a href="http://docs.mongodb.org/manual/tutorial/modify-documents/" rel="nofollow noreferrer"><strong>multi</strong></a> document update mode.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is intended as a query and I really mean when the <strong>default</strong> attribute is not set I want the first element in the array as it is listed. Every time.</p>
  
  <p>The strings are sample data so don't rely on lexical order. All <code>object#</code> references are likely real <strong>$oid</strong> in the real world.</p>
  
  <p>This may end up as a bounty. Schema changes are accepted within the tolerance of the update as mentioned. At worst the findings are a reasonable basis for a JIRA issue.</p>
  
  <p>For reference, I launched this based out of thinking from my answer on <a href="https://stackoverflow.com/questions/21521728/how-do-i-implement-this-mongodb-query-update-operation-csharp-driver/21524313#21524313">this post</a>, which is largely about re-thinking the schema to accommodate the goal.</p>
  
  <p>Good hunting.</p>
  
  <p>P.S And <a href="http://www.mongodb-is-web-scale.com/" rel="nofollow noreferrer">Webscale</a>, people. Updates on the collection need to happen without iteration as there could be a really, really, ( oh webscale! ) big number of them.</p>