title: Mongoose: How to query for a referenced object property?
body: <p>I'm trying to get a list of documents from my database based on the property a sub-document. The models and schemas I'm using are:</p>
  
  <pre><code>var elementSchema = new mongoose.Schema({
      name: String,
      description: String,
  
      _story: { type: mongoose.Schema.Types.ObjectId, ref: 'Story' },
  
      date_created: Date,
      date_modified: Date,
  };
  var storySchema = new mongoose.Schema({
      title: {type: String, default: '', trim: true},
      isPrivate: {type: Boolean, default: false},
  });
  
  mongoose.model("Story", storySchema);
  mongoose.model("Element", elementSchema);
  </code></pre>
  
  <p>I'm trying to get all Elements that belong to a Story that is not private, and according to some posts I saw around here (<a href="https://stackoverflow.com/questions/10214231/mongoose-retrieving-object-from-ref-query">1</a>, <a href="https://stackoverflow.com/questions/5772982/finding-an-embedded-document-by-a-specific-property-in-mongoose-node-js-mongod">2</a>, <a href="https://stackoverflow.com/questions/12587147/mongoosejs-using-find-to-find-by-reference">3</a>) the solution would be to use _story.isPrivate with find. I'm currently doing this:</p>
  
  <pre><code>Element.find({'_story.isPrivate': false})
           .populate('_story')
           .exec(function(err, elements){
               if(err){
                   return next(err);
               }
               else if(elements.length &gt; 0){
                   return res.send(elements);
               }
               else{
                   return res.send(404, {message: "No elements found"});
               }
  });
  </code></pre>
  
  <p>But the result is always an empty set (returns 404). With no condition, find returns all elements and populates _story correctly. I also activated the debug output to see the queries being executed and I get this:</p>
  
  <pre><code>Mongoose: elements.find({ '_story.isPrivate': false }) { fields: undefined, safe: undefined }
  </code></pre>
  
  <p>Trying to execute this in MongoDB I get no results. What can be wrong here?</p>
  
  <p>Thanks</p>