title: Select messages where author is marked as "allow others to see that user's commets"
body: <p>I'm looking at switching to MongoDB at some point for some of my projects. For that to happen I need to test it and know what I can and what I can't do.</p>
  
  <p>After reading some books about how does MongoDB works I must say I'm a little bit confused about how does actually the <code>select</code>'s work.</p>
  
  <p>Let's say I have a collection called <code>users</code>:</p>
  
  <pre><code>{
      user_name: string,
      ....,
      ....,
      messages_can_be_seen_by_others: boolean
  }
  </code></pre>
  
  <p>also, I have a collection of <code>topics</code>:</p>
  
  <pre><code>{
      topic_title: string,
      .....,
      .....,
      topic_messages:
          [
              {
                   user_name: string,
                   text: string,
                   date: DateTime
              },
              {
                   user_name: string,
                   text: string,
                   date: DateTime
              }
          ]
      .....
  }
  </code></pre>
  
  <p>How can I select one topic and all it's messages according to the <code>users</code> collection?
  I mean, a message writen by the person <code>Person1</code> who does not have the <code>messages_can_be_seen_by_others</code> marked as <code>true</code> should not be returned by the database.</p>
  
  <p>So, should I use map/reduce, filter with <code>php</code> once I have all the results, or is there any other thing I'm not aware of?
  In case I should use map/reduce how difficult could it be?</p>
  
  <p>Also, there's another question. How can I select all the fields from <code>topics</code> and the <code>date</code> from the first message? (Something like <code>$elemMatch.messages.0.date</code>)</p>
  
  <p>PS: Extra question: Can I return the number of messages that a topic has? (without actually counting them using PHP. Just plan MongoDB select statement)</p>
  
  <p>Regards</p>