title: Mongodb query collection b based on output of collection A
body: <p>I Have <code>2</code> collections in my DB</p>
  
  <p><strong>Collection_A :</strong></p>
  
  <pre><code>{
      _id: ObjectId("51156a1e056d6f966f268f82"),
      name: "priya",
      created_time: "16-07-2015 11:05",
      group_id: "648856e9e5304d"
  },
  {
      _id: ObjectId("51156a1e056d6f966f268f81"),
      name: "Derick Rethans",
      created_time: "17-07-2015 11:05",
      group_id: "8856e9e5304d"
  }
  </code></pre>
  
  <p><strong>Collection B :</strong></p>
  
  <pre><code>{
  group_id : "648856e9e5304d"
  group_name: "Art"
  }
  </code></pre>
  
  <p>I want to write a python script which will query <code>collection A</code> and get the documents based on time (like docments from <code>16</code> to <code>17</code>). And query <code>Collection B</code> for <code>group_name</code> using <code>group_id</code> from <code>collection A</code>. Below is my code where im writing the output into <code>csv</code> file. </p>
  
  <pre><code>with open('example.csv', 'a') as f:
     for x in db.Collection_A.find():
      w = csv.DictWriter(f, x.keys())
      w.writeheader()
      w.writerow(x)
  </code></pre>
  
  <p>How to alter my code to output range by <code>date</code> and <code>group_name</code>.</p>