title: MongoDB query on populated fields
body: <p>I have models called "Activities" that I am querying for (using Mongoose). Their schema looks like this:</p>
  
  <pre><code>var activitySchema = new mongoose.Schema({
      actor: {
          type: mongoose.Schema.ObjectId,
          ref: 'User',
          required: true
      },
      recipient: {
          type: mongoose.Schema.ObjectId,
          ref: 'User'
      },
      timestamp: {
          type: Date, 
          default: Date.now
      },
      activity: {
          type: String,
          required: true
      },
      event: {
          type: mongoose.Schema.ObjectId,
          ref: 'Event'
      },
      comment: {
          type: mongoose.Schema.ObjectId,
          ref: 'Comment'
      }
  });
  </code></pre>
  
  <p>When I query for them, I am populating the <code>actor</code>, <code>recipient</code>, <code>event</code>, and <code>comment</code> fields (all the references). After that, I also deep-populate the <code>event</code> field to get <code>event.creator</code>. Here is my code for the query:</p>
  
  <pre><code>var activityPopulateObj = [
                  { path: 'event' },
                  { path: 'event.creator' },
                  { path: 'comment' },
                  { path: 'actor' },
                  { path: 'recipient' },
                  { path: 'event.creator' }
              ],
              eventPopulateObj = {
                  path: 'event.creator',
                  model: User
              };
  
  Activity.find({ $or: [{recipient: user._id}, {actor: {$in: user.subscriptions}}, {event: {$in: user.attending}}], actor: { $ne: user._id} })
              .sort({ _id: -1 })
              .populate(activityPopulateObj)
              .exec(function(err, retrievedActivities) {
                  if(err || !retrievedActivities) {
                      deferred.reject(new Error("No events found."));
                  }
                  else {
                      User.populate(retrievedActivities, eventPopulateObj, function(err, data){
                          if(err) {
                              deferred.reject(err.message);
                          }
                          else {
                              deferred.resolve(retrievedActivities);
                          }
                      });
                  }
              });
  </code></pre>
  
  <p>This is already a relatively complex query, but I need to do even more. If it hits the part of the <code>$or</code> statement that says <code>{actor: {$in: user.subscriptions}}</code>, I <strong>also</strong> need to make sure that the <code>event</code>'s <code>privacy</code> field is equal to the string <code>public</code>. I tried using <code>$elemMatch</code>, but since the event has to be populated first, I couldn't query any of its fields. I need to achieve this same goal in multiple other queries, as well.</p>
  
  <p>Is there any way for me to achieve this further filtering like I have described?</p>