pymongo


Conditional atomic mongo remove with different results


There's a collection of items in mongo database. I want to implement a method that removes an item, provided its id.
However, I only want to remove the item if it has, say, a deletable property set to true. Then, I want to distinguish the following situations (errors) to handle them different:
The item exists and the deletable property set to true => remove it
The item exists and the deletable property set to false => don't remove it, raise Error1
The item does not exist => nothing to remove, raise Error2
Assuming the application is multithreaded and there might be several workers adding, removing and modifying items, is there a way to handle such case correctly?
As for now, I can either remove or skip silently an item with specified id and deletable property or issue a few queries one by one to handle all situations, but it might not thread safe.
I'm using pymongo to access the database if it matters.
You will never get this in a single statement as there will be no differentiation between "how many" fields matched your conditions. But you can just do this with logical conditions and an additional find where the first attempt did not remove any documents. Just use the write concern response to see what happened.
resp = db.collection.remove({ 'id': id, 'deletable': True }, w=1)
if resp['n'] == 0:
doc = db.collection.find_one({ 'id': id, 'deletable': False })
if doc is None:
# this would be not found 404
else:
# this would the not deletable
else:
# this would be deleted 200
So checking the returned value with any write concern applied let's you know if the delete occurred and the following find_one let's you know if the document simply was not found or was just marked as deletable false at the time.

Related Links

How do I call a function with pymongo using webpy?
python motor offers fetch_next exception on aggregation
python motor mongo cursor length or peek next
PyMongo check if update succeeded
insert and query an OrderedDict in MongoHQ
Return .str of ObjectID using pymongo
Selecting the lowest/highest with find_one in Pymongo
Safe use of Pymongo's find_one
Pymongo $in + $regex
How can I get pymongo to always return str and not unicode?
SON Manipulator in mongo queries
How to append a document to a 2nd level embedded model list document in Django and mongoengine
pymongo multiple update $in
How to get negetive of a complex where clause in mongo db
pymongo justOne & multi
How to make pymongo's find() return a list?

Categories

HOME
mod-rewrite
cucumber
eclipse-plugin
d3.js
stacktrace.js
android-emulator
consul
yql
azure-data-lake
impala
wagtail
xamarin-zebble
tiff
documentum
multi-step
sslhandshakeexception
ex
corona
google-tasks-api
nest-api
closures
bsd
neo4j.rb
maven-surefire-plugin
pdf.js
software-packaging
jstree
cellular-network
beego
google-play-developer-api
source-insight
autodesk-model-derivative
mongodb-3.4
om-next
quickcheck
adminer
dreamfactory
subclassing
redgate
djcelery
elastica
xll
llvm-ir
assert
flowchart
data-uri
haar-classifier
transparency
git-tfs
sendinput
audio-converter
destroy
daz3d
roracle
xcglogger
password-hash
entropy
watir-webdriver
word-2007
sesame
word-2016
oid
jsonix
openquery
pyaudio
nunit-console
tsqlt
piping
maximize
as3-api
voronoi
html.actionlink
arbre
self
apple
supersocket.net
webproject
nhibernate-criteria
angular-ui-select
squeezebox
sqlbindparameter
jack
roxy-fileman
spring-social-linkedin
smips
illuminate-container
virtual-channel
bcdedit
blobstorage
days
semicolon
towers-of-hanoi
system-information
jzmq
monocross
exponent
differentiation
azure-acs
uninstaller
rational-unified-process
gui-designer

Resources

Encrypt Message