title: Programmatically build dynamic query with Mongoose
body: <p>I'm trying to build a search from input received from a form. </p>
  
  <pre><code>router.get('/data', function(req, res) {
      var firstName=req.body.firstName,
      lastName=req.body.lastName,
      companyName=req.body.companyName,
      email=req.body.email;
  });
  </code></pre>
  
  <p>I'd like to build a query up from these values, but if the field has no value, I obviously don't want to include it in the search (searching for "" would change the results)</p>
  
  <p>I've tried a couple different things, like building a string to place in:</p>
  
  <pre><code>mongoose.model('customers').find({QUERY STRING WOULD GO HERE} ,function(err, data) {
      if (err) return console.error(err);
      console.log(data);
  });
  </code></pre>
  
  <p>But that doesn't seem to work properly. I also tried "stacking" search queries like this: </p>
  
  <pre><code>if(firstName !="") {
      mongoose.model('customers').find({firstName: firstName})
  }
  </code></pre>
  
  <p>and then executing the search like this:</p>
  
  <pre><code>mongoose.model('customers').exec(function(err, customer){
      console.log(customer);
  });
  </code></pre>
  
  <p>But that causes 500 errors (and I'm not sure if there's any more info I can get from them).</p>
  
  <p>Please help a Newbie dynamically build a mongoose search query :(</p>