Reputation: 67
I have a mongodb instance running and I need to change the '_id' field of each document. Therefore I retrieve documents that need to changed, modified the '_id', inserted the modified documents, and deleted the original document. Everything succeeds, but I am unable to see the change in the database.
doc_id = doc['_id']
inserted = db.candidates.insert_one(doc)
deleted = db.candidates.delete_one({'_id': doc_id})
res = list(db.candidates.find({'_id': inserted.inserted_id}))
assert len(res) >= 1, 'could not retrieve inserted document'
The assertion on line 4 fails for every document. And I can find the deleted document in the database even though the DeleteResult reports that it was deleted.
{'n': 1, 'ok': 1.0}
I have been unable to find anything in the Pymongo documentation about this issue and I have been unable to find similar problems online. Am I missing something obvious?
Modification of other collections and fields seems to work fine.
Upvotes: 0
Views: 213
Reputation: 8814
You're inserting the document, then deleting the same document. That's why your find doesn't return any records.
Upvotes: 1