title: MongoDB: Filter using data from multiple collections
body: <p>I have an application that stores users and their behavior in the form of events. There are two collections, one for the users and one for the events. The documents look like this:</p>
  
  <p><strong>User</strong></p>
  
  <pre><code>{
      "_id" : ObjectId("593aa71e2f9d5140000bb44e"),
      "name" : "Antonette Ortiz",
      "country" : "France"
  }
  </code></pre>
  
  <p><strong>Event</strong></p>
  
  <pre><code>{
      "_id" : ObjectId("593aaa84c685604066a6a0cf"),
      "name" : "message_sent",
      "timestamp" : ISODate("2016-11-01T04:39:52.667Z"),
      "user" : ObjectId("593aa728d135484002399bac"),
      "attributes" : {
          "str" : "plum",
          "int" : 82
      }
  }
  </code></pre>
  
  <p>Now I want to be able to fetch a list of users not only by their attributes, but also in regard to the events they have triggered and with respect to a certain timeframe.</p>
  
  <p>An example query would be something like: <strong>"All users from France that have at least sent 3 messages within the last 7 days".</strong></p>
  
  <p>How can I achieve this using MongoDB, also in regard to performance (if I have several million events for example)? Is this even possible with just the two collections, or do I have to use aggregation / map reduce? If so, what would you recommend how to change in the schema?</p>