title: Mongoose Query: Find an element inside an array
body: <p>Mongoose/Mongo noob here:</p>
  
  <p><strong>My Data</strong></p>
  
  <p>Here is my simplified data, each user has his own document</p>
  
  <pre><code>{ "__v" : 1,
    "_id" : ObjectId( "53440e94c02b3cae81eb0065" ),
    "email" : "test@test.com",
    "firstName" : "testFirstName",
    "inventories" : [ 
      { "_id" : "active",
        "tags" : [ 
          "inventory", 
          "active", 
          "vehicles" ],
        "title" : "activeInventory",
        "vehicles" : [ 
          { "_id" : ObjectId( "53440e94c02b3cae81eb0069" ),
            "tags" : [ 
              "vehicle" ],
            "details" : [ 
              { "_id" : ObjectId( "53440e94c02b3cae81eb0066" ),
                "year" : 2007,
                "transmission" : "Manual",
                "price" : 1000,
                "model" : "Firecar",
                "mileageReading" : 50000,
                "make" : "Bentley",
                "interiorColor" : "blue",
                "history" : "CarProof",
                "exteriorColor" : "blue",
                "driveTrain" : "SWD",
                "description" : "test vehicle",
                "cylinders" : 4,
                "mileageType" : "kms" } ] } ] }, 
      { "title" : "soldInventory",
        "_id" : "sold",
        "vehicles" : [],
        "tags" : [ 
          "inventory", 
          "sold", 
          "vehicles" ] }, 
      { "title" : "deletedInventory",
        "_id" : "deleted",
        "vehicles" : [],
        "tags" : [ 
          "inventory", 
          "sold", 
          "vehicles" ] } ] }
  </code></pre>
  
  <p>As you can see, each user has an <code>inventories</code> property that is an array that contains 3 inventories (activeInventory, soldInventory and deletedInventory)</p>
  
  <p><strong>My Query</strong></p>
  
  <p>Given an user's email a a vehicle ID, i would like my query to go through find the user's <code>activeInventory</code> and return just the vehicle that matches the ID. Here is what I have so far:</p>
  
  <pre><code>user = api.mongodb.userModel;
  ObjectId = require('mongoose').Types.ObjectId;
  return user
      .findOne({email : params.username})
      .select('inventories')
      .find({'title': 'activeInventory'})
      //also tried
      //.where('title')
      //.equals('activeInventory')
      .exec(function(err, result){
          console.log(err);
          console.log(result);
      });
  </code></pre>
  
  <p>With this, result comes out as an empty array. I've also tried <code>.find('inventories.title': 'activeInventory')</code> which strangely returns the entire inventories array. If possible, I'd like to keep the chaining query format as I find it much more readable.</p>
  
  <p><strong>My Ideal Query</strong></p>
  
  <pre><code>return user
      .findOne({email : params.username})
      .select('inventories')
      .where('title')
      .equals('activeInventory')
      .select('vehicles')
      .id(vehicleID)
      .exec(cb)
  </code></pre>
  
  <p>Obviously it does not work but it can give you an idea what I'm trying to do.</p>