title: A better method for counting documents with object ID's using Mongoose?
body: <p>Currently I'm querying MongoDB with Mongoose to search for a particular category with a specific ObjectID (e.g. 5ca510c87cf5db0017a59d5d) and then count the documents in a Process with that particular category. However, I'm wondering if there is a better way to do this that can query the <code>name</code>  field (e.g. 'Primary') of category instead, that way it can work across any other database without modifying the code to a specific ID.</p>
  
  <p>This is a Node.js application with Express being used for MVC and Mongoose for ODM.</p>
  
  <p>I've referred to Mongoose's <code>populate</code> function in the <a href="https://mongoosejs.com/docs/populate.html" rel="nofollow noreferrer" title="Mongoose Populate Documentation">documentation</a>, but can't seem to find a good analogy.</p>
  
  <p>Category Model:</p>
  
  <pre><code>var CategorySchema = new Schema({
      name: {type: String, required: true, min: 3, max: 100}
  });
  </code></pre>
  
  <p>Process Model:</p>
  
  <pre><code>var ProcessSchema = new Schema(
    {
      name: {type: String, required: true},
      machine: {type: Schema.Types.ObjectId, ref: 'Machine', required: true},
      summary: {type: String, required: true},
      serial_number: {type: String, required: true},
      category: [{type: Schema.Types.ObjectId, ref: 'Category'}]
    }
  );
  </code></pre>
  
  <p>Process Controller:</p>
  
  <pre><code>exports.index = function(req, res) {
  
      async.parallel({
          ...
          process_category_primary_count: function(callback) {
              Process.countDocuments({category: '5ca510c87cf5db0017a59d5d'},callback);
          },
          process_category_intermediate_count: function(callback) {
              Process.countDocuments({category: '5ca510f57cf5db0017a59d5e'},callback);
          },
          process_category_finishing_count: function(callback) {
              Process.countDocuments({category: '5ca511017cf5db0017a59d5f'},callback);
          },
          ...
      }, function(err, results) {
          res.render('index', { title: 'Dashboard Home',
                                error: err,
                                data: results });
      });
  };
  </code></pre>
  
  <p>By default a <code>find</code> all query for process will produce a result like this:</p>
  
  <pre><code>{ category: [ 5c9d48508ee1353a2786c7d0 ],
      _id: 5ca3ca9a9743b52602d88c57,
      name: 'Milling-Detailing',
      machine: 5c9e407c6e0b26209b55dbbd,
      summary: 'Detailing pass for each object.',
      serial_number: '1002',
      __v: 0 }
  </code></pre>
  
  <p>So in essence: I'd like to use a Category 'name' rather than an 'id' to search and count the Process collection.</p>