title: MongoDB: Query and retrieve objects inside embedded array?
body: <p>Let's say I have the following document schema in a collection called 'users':</p>
  
  <pre><code>{
      name: 'John',
      items: [ {}, {}, {}, ... ]
  }
  </code></pre>
  
  <p>The 'items' array contains objects in the following format: </p>
  
  <pre><code>{
      item_id: "1234",
      name: "some item"
  }
  </code></pre>
  
  <p>Each user can have multiple items embedded in the 'items' array.</p>
  
  <p>Now, I want to be able to fetch an item by an item_id for a given user.</p>
  
  <p>For example, I want to get the item with id "1234" that belong to the user with name "John".</p>
  
  <p>Can I do this with mongoDB?  I'd like to utilize its powerful array indexing, but I'm not sure if you can run queries on embedded arrays and return objects from the array instead of the document that contains it.</p>
  
  <p>I know I can fetch users that have a certain item using {users.items.item_id: "1234"}.  But I want to fetch the actual item from the array, not the user.</p>
  
  <p>Alternatively, is there maybe a better way to organize this data so that I can easily get what I want?  I'm still fairly new to mongodb.</p>
  
  <p>Thanks for any help or advice you can provide.</p>