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

Displaying nested dictionaries in flask-admin
mongoengine slow serialization of embedded documents with reference fields
Pymongo error : 'Cursor' object has no attribute 'find' , how to filter it?
Spark Streaming: foreachRDD insert into mongoDB using python?
how to find right version of bson from pip for pymongo/mongoengine
How to enable `projection` in pymongo query class `find()`?
Variables don't hold value for very long pymongo ipython
pymongo upgrade to 3.0.3 causes this statement to fail
UUID to NUUID in Python
how to test for empty mongodb query in pymong
read video file from mongodb with pymongo
Get OperationFailure error with PyMongo 2.7.1
Inserting string as regular string in mongodb
Mongos + Pymongo 2.5 ==>No suitable hosts found
pymongo collection add item using update
How to convert objectid to string

Categories

HOME
typelite
shinyapps
mongoid
openflow
swift2
wagtail
selection
reportviewer
stack-overflow
elastic-load-balancer
lc3
twitter-oauth
migrate
scrape
phpstorm-2017.1
circleci
multi-step
stimulsoft
pushdown-automaton
jodatime
fosuserbundle
cocos2d-android
robolectric
tfsbuild
infrared
jest
dashdb
sipp
unordered-multimap
tf-idf
jackrabbit-oak
intune
universal
gnu-classpath
cmis
des
numerics
cordys-opentext
jslint
nanogallery
cartopy
strapi
gitolite
swfupload
icepdf
pyttsx
word-cloud
web-inspector
expandablelistview
oracle-bmcs
gcal
liteide
uiautomatorviewer
nonlinear-functions
watir-webdriver
dex
cognos-tm1
yargs
ternary-operator
dcount
data-management
nvda
spring-mongo
qtcpsocket
maximize
mnesia
currency-exchange-rates
teensy
highest
scala-macros
deque
mgwt
camus
html-escape-characters
baseadapter
thoughtworks-go
flurry-analytics
window-resize
visual-studio-6
loginview
aho-corasick
zend-search-lucene
usb-flash-drive
django-sites
pbkdf2
vlab
iirf
form-for
eager-loading
story
audiotoolbox
msinfo32
multidrop-bus
qtextbrowser
groovy-console
kext
mediacontroller
bll
qcar-sdk
printqueue
regression-testing
aggregator
ixmldomdocument
3270

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App