title: Retrieving MongoDB Subdocuments with Node.js and Express.js
body: <p>I created a RESTful API using node.js, express.js, and mongodb. I started creating my routes by pulling documents from a MongoDB collection which work perfectly.</p>
  
  <p><strong>Example Collection Document</strong></p>
  
  <pre><code>{ 
  "_id" : ObjectId("51ace8c04cc8ea865df0923e"), 
  "title" : "Some Example Title", 
  "producer" : 
   { 
    "company" : "Your Company Name"
   } 
  }
  </code></pre>
  
  <p><strong>Works</strong> - and it also works if I do a .find({query}) instead of a generic find()</p>
  
  <pre><code>   app.get('/something', something.findAll);
      exports.findAll = function(req, res) {
          db.collection('something', function(err, collection) {
              collection.find().toArray(function(err, items) {
                  res.contentType('json');
                  res.header("Access-Control-Allow-Origin", "*");
                  res.header('Access-Control-Allow-Methods', 'GET, PUT');
                  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
                  res.send(items);
              });
          });
      };
  </code></pre>
  
  <p>But when I try to call a  embedded document (i.e. subdocument) it using dot.notation it breaks.</p>
  
  <p>*<strong><em>Doesn't Work</em>*</strong></p>
  
  <pre><code>db.something.find( { 'producer.company': 'ABC123' } )
  </code></pre>
  
  <p>Or even if I try</p>
  
  <pre><code>db.something.find( {producer: {company: 'ABC123'} } );
  </code></pre>
  
  <p>I get an error message saying.</p>
  
  <pre><code>TypeError: Converting circular structure to JSON
      at Object.stringify (native)
      at ServerResponse.res.json (../lib/response.js:185:19)
      at ServerResponse.res.send (..//lib/response.js:117:21)
      at ../routes/recipes.js:81:8
      at Collection.find (../node_modules/mongodb/lib/mongodb/collection.js:931:5)
      at ../routes/recipes.js:73:14
      at Db.collection (../lib/mongodb/db.js:462:44)
      at exports.findByCat (../routes/recipes.js:72:5)
      at callbacks (../node_modules/express/lib/router/index.js:161:37)
      at param (../node_modules/express/lib/router/index.js:135:11)
  </code></pre>
  
  <p>Can anybody help me find a workaround, or let me know if there are any errors with my approach.</p>
  
  <p>Thanks!</p>