简体   繁体   中英

mongodb bulk remove and insert

i am learning python with mongodb in torando

my data is

[
 {
    "_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"
 }
]

i have tried this code for bulk remove

 bulk = db.calendar.initializeUnorderedBulkOp()
 bulk.find({"Period": p}).remove()
 bulk.execute()

i also need to insert another set of data which i earlier inserted as

db.calendar.insert(data)

which is stored in the db as

 {
    "_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"
 }

And the first set of data

{"Pid" : "566bb17761be0b1e0059c09d",
 "EndDate" : "10-12-2015",
 "StartDate" : "5-12-2015",
 "Registration" : "TN 64 KS 7777",
 "Status" : "Booked"}

is stored repeatedly.

i would like to use bulk operation to solve this problem.

Can anyone tell me how to do it

I tried it by doing this

 p = "12-2015"

 bulk = db.calendar.initializeUnorderedBulkOp()
 bulk.find({"Period": p}).remove()
 bulk.insert(data)
 bulk.execute()

but got error.

Edit:

I would like to use bulk because i have to access the same collection twice in a single connection.

For bulk deletion, you don't need anything special. This should be fine:

db.calendar.remove({"Period": p})

Or if you have already fetched the records for an earlier use, you can do this:

records = db.calendar.find({"Period": p})
records.remove()

You can use insert_many to insert the data in one query.

db.calendar.insert_many(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM