0

I have a script that store Data in a MongoDB, and I'd like to delete document with another script. Each document stored in my DB comes in this format :

{"k1526346000_500": 
      {"r45037": {"C": "1", "V": "1000", "L": "1181", "D": "75"},
      {"r21542": {"C": "2", "V": "94527", "L": "105", "D": "94"},
      etc...
}

What I'd like to do is to delete a document by passing the key (k1526346000_500 in this case) When I try to do :

db.collection.delete_one({_id:key}) 

Where key corresponds to k1526346000_500, I have an error :

Exception in thread Thread-1:
   Traceback (most recent call last):
   File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
   self.run()
   File "fetch.py", line 38, in run
       read(self.mongo).test(key)
       File "/var/python/lib/script.py", line 121, in test
       db.collection.delete_one({_id:key})
   NameError: global name '_id' is not defined

BUT, the document is correctly deleted. It's a problem for me because the error exits the script, and I have to make it run in an loop.

Do you know how can I make it work ?

Thanks ;)

EDIT : When I dump that document, I have this result :

{ "_id" : ObjectId("5afda3efb2031025afe0d32e"), "k1526346000_500" : { "r4" : { "C" : "1", "V" : "447616", "L" : "1", "D" : "4" }

So this is definitely not "_id" to use here, but I don't know how to catch this field :(

akioz
  • 53
  • 5
  • First I generated all the lines like : {"r45037": {"C":....... Then I created a dict like that : dict['k1526346000_500'] = rows where rows is already a dict. So I assumed that when I created that key, it goes directly in '_id' doesn't it ? Edit : If it's not an "_id" field, what it could be ? – akioz May 17 '18 at 15:39
  • You're right mate, here's the result : { "_id" : ObjectId("5afda3efb2031025afe0d32e"), "k1526346000_500" : { "r4" : { "C" : "1", "V" : "447616", "L" : "1", "D" : "4" }, "r5" : { "C" : "1", "V" : "398949", "L" : "1", "D" : "5" }, "r6" : { "C" : "1", "V" : "9276983", "L" : "1", "D" : "6" }, "r7" : { "C" : "1", "V" : "1544083", "L" : "1", "D" : "7" }, "r0" : { "C" : "1", "V" : "471757766", "L" : "1", "D" : "0" }, "r1" : { "C" : "1", "V" : "3473966", "L" : "1", "D" : "1" }, "r2" : { "C" : "1", "V" : "3396966", "L" : "1", "D" : "2" }} So how can I call this ? – akioz May 17 '18 at 15:47

3 Answers3

0

Python is not JavaScript. Keys need to be quoted:

db.collection.delete_one({"_id": key}) 
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks for your answer ! I didn't say it but I tested this, and I don't have any error, but the document isn't removed – akioz May 17 '18 at 15:32
0

Are you actually specifying the name of the collection? Eg if your collection is called tree, you need to say

db.tree.deleteOne({_id:key})

Also make sure you are connected to the correct database- eg, use forest.

Frank
  • 328
  • 1
  • 2
  • 10
  • Hi ! Thanks for your answer. I actually specify the name of the collection, I just replaced it here. I make several actions before trying to delete, and all those actions work, so the parameters passed are right ! – akioz May 17 '18 at 15:35
0

"k1526346000_500" is the name of a field within the document, not the document's _id value. So assuming that's a unique key name, you can remove that document by searching for it with the $exists operator:

db.collection.delete_one({key: {"$exists": True}})
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471