title: MongoDB: Find an element in an array
body: <p>I have a simple array in my MongoDB collection:</p>
  
  <pre><code>"_id": "5669b4930874f8d01f7676da", "cars": ["BMW", "Audi", "VW"]
  </code></pre>
  
  <p>I now want to check if "Audi" is in that document. I've read through all questions on arrays here at SO but they focus all on array with objects inside, which isn't the case here.</p>
  
  <pre><code>db.collectionName.find({ cars: {$eq: "Audi"} })
  </code></pre>
  
  <p>doesn't work (it returns no document).</p>
  
  <p>EDIT: It was my own stupidity, see my comment below on the best solution. So the above query was <strong>always</strong> working. Well, as they say the problem always sits in front of the keyboard ;-)</p>
  
  <p>Neither does:</p>
  
  <pre><code>db.collectionName.find({ $elemMatch: { cars: "Audi" }})
  </code></pre>
  
  <p>It actually leads to the following error message:</p>
  
  <pre><code>error: {
  "$err" : "Can't canonicalize query: BadValue unknown top level operator: $elemMatch",
  "code" : 17287
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>db.collectionName.find({ cars: { $in: "Audi" }})
  </code></pre>
  
  <p>This leads to the following error:</p>
  
  <pre><code>error: {
  "$err" : "Can't canonicalize query: BadValue $in needs an array",
  "code" : 17287
  </code></pre>
  
  <p>Tested with MongoDB version 2.6.7 via MongoDB shell. Thanks for your help!</p>