title: mongodb bulk remove and insert
body: <p>i am learning  python with mongodb in torando</p>
  
  <p>my data is</p>
  
  <pre><code>[
   {
      "_id" : ObjectId("5671350061be0b0a6454d68e"),
      "Pid" : "566bb17761be0b1e0059c09d",
      "EndDate" : "10-12-2015",
      "StartDate" : "5-12-2015",
      "Registration" : "TN 64 KS 7777",
      "Status" : "Booked"
       "Period": "12-2015"
   }
   {
      "_id" : ObjectId("5671350061be0b0a6454d68f"),
      "Pid" : "566bb17761be0b1e0059c09d",
      "EndDate" : "20-12-2015",
      "StartDate" : "15-12-2015",
      "Registration" : "TN 64 KS 7777",
      "Status" : "NA"
      "Period": "12-2015"
    }
   {
      "_id" : ObjectId("5671350061be0b0a6454d690"),
      "Pid" : "566bb17761be0b1e0059c09d",
      "EndDate" : "24-12-2015",
      "StartDate" : "21-12-2015",
      "Registration" : "TN 64 KS 7777",
      "Status" : "AOs"
   }
   {
      "_id" : ObjectId("5671350061be0b0a6454d691"),
      "Pid" : "566bb17761be0b1e0059c09d",
      "EndDate" : "30-12-2015",
      "StartDate" : "25-12-2015",
      "Registration" : "TN 64 KS 7777",
      "Status" : "AOr"
      "Period": "12-2015"
   }
  ]
  </code></pre>
  
  <p>i have tried this code for bulk remove</p>
  
  <pre><code> bulk = db.calendar.initializeUnorderedBulkOp()
   bulk.find({"Period": p}).remove()
   bulk.execute()
  </code></pre>
  
  <p>i also need to insert another set of data which i earlier inserted as </p>
  
  <pre><code>db.calendar.insert(data)
  </code></pre>
  
  <p>which is stored in the db as </p>
  
  <pre><code> {
      "_id" : ObjectId("5671350061be0b0a6454d68e"),
      "Pid" : "566bb17761be0b1e0059c09d",
      "EndDate" : "10-12-2015",
      "StartDate" : "5-12-2015",
      "Registration" : "TN 64 KS 7777",
      "Status" : "Booked"
   }
   {
      "Pid" : "566bb17761be0b1e0059c09d",
      "EndDate" : "10-12-2015",
      "StartDate" : "5-12-2015",
      "Registration" : "TN 64 KS 7777",
      "Status" : "Booked"
   }
   {
      "_id" : ObjectId("5671350061be0b0a6454d690"),
      "Pid" : "566bb17761be0b1e0059c09d",
      "EndDate" : "10-12-2015",
      "StartDate" : "5-12-2015",
      "Registration" : "TN 64 KS 7777",
      "Status" : "Booked"
   }
   {
      "_id" : ObjectId("5671350061be0b0a6454d691"),
      "Pid" : "566bb17761be0b1e0059c09d",
      "EndDate" : "10-12-2015",
      "StartDate" : "5-12-2015",
      "Registration" : "TN 64 KS 7777",
      "Status" : "Booked"
   }
  </code></pre>
  
  <p>And the first set of data </p>
  
  <pre><code>{"Pid" : "566bb17761be0b1e0059c09d",
   "EndDate" : "10-12-2015",
   "StartDate" : "5-12-2015",
   "Registration" : "TN 64 KS 7777",
   "Status" : "Booked"}
  </code></pre>
  
  <p>is stored repeatedly.</p>
  
  <p>i would like to use <code>bulk</code> operation to solve this problem.</p>
  
  <p>Can anyone tell me how to do it</p>
  
  <p>I tried it by doing this </p>
  
  <pre><code> p = "12-2015"
  
   bulk = db.calendar.initializeUnorderedBulkOp()
   bulk.find({"Period": p}).remove()
   bulk.insert(data)
   bulk.execute()
  </code></pre>
  
  <p>but got error.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I would like to use bulk because i have to access the same collection twice in a single connection.</p>