pymongo


pymongo justOne & multi


I have a database "testdb" with collection "testcol" which looks like this:
{u'_id': ObjectId('50eeb8029b75941b9af614bd'), u'birth': 1}
{u'_id': ObjectId('50eeb82e9b75941bc820f22c'), u'birth': 2}
{u'_id': ObjectId('50eeb82e9b75941bc820f22d'), u'birth': 3}
{u'_id': ObjectId('50eeb82f9b75941bce96032c'), u'birth': 2}
{u'_id': ObjectId('50eeb82f9b75941bce96032d'), u'birth': 3}
The code is like:
m_connection = MongoClient(M.HOST,M.PORT)
col = m_connection['testdb']['testcol']
#some_operation
cursor = col.find()
for doc in cursor:
print doc
Im trying to update all the documents with birth higher than one,
so I replace some_operation with col.update({'birth':{'$gt':1}},{'$set':{'death':'near'}},{'multi':True}) I get the error: TypeError: upsert must be an instance of bool
Similarly, I want to delete only one out of all the documents that have birth as 2 0r 3.
I replace some_operation with col.remove({'birth' : {'$in' : [2,3]}},{'justOne' : 1}) I get the error: TypeError: Wrong type for safe, value must be a boolean
I tried using direct booleans in place of {} array, but only one document is update and all those with birth as 2 or 3 are deleted.
Just in case it has something to do with versions: I have pymongo-2.4.1 and Python2.7
Any clues where im going wrong??
Thanks a lot.
Edit:
The documentation says:
update(spec, document[, upsert=False[, manipulate=False[, safe=None[, multi=False[, check_keys=True[, **kwargs]]]]]])
Does it mean that: if i want to use 'multi'=True, i have to compulsarily go around defining values for whatever (upsert, manipulate, safe) comes before it?
Also, for remove, it says:
remove([spec_or_id=None[, safe=None[, **kwargs]]])
Where is the 'justOne' mentioned here??
As long as you use multi= in your call to update() (e.g., col.update({'birth':{'$gt':1}}, {'$set':{'death':'near'}}, multi=True)), Python will apply the default values for any optional arguments you omit. You can read more about this behavior at http://docs.python.org/2/tutorial/controlflow.html#keyword-arguments
To your other question about justOne, unfortunately it does not appear as if PyMongo supports it yet. Currently the way to do this would be to use find_one() to get the document and then pass its _id to remove().

Related Links

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
Conditional atomic mongo remove with different results
python script to connect to mongodb master
flask-admin and pymongo search
Running code when mongoengine loads a document
Can't replace mongo document
How do I call a function with pymongo using webpy?
python motor offers fetch_next exception on aggregation

Categories

HOME
amazon-web-services
oracle
orientdb
svn
opencart
android-fragments
exchange-server
subdomain
risk-management
odata
flurry
apache-kafka-connect
stored-procedures
ag-grid
firebase-dynamic-links
cdi
xades4j
match
locationmanager
alljoyn
phpstorm-2017.1
smartgwt
google-awareness
tomcat8
rpmbuild
spring-shell
altium-designer
quantitative-finance
pyopencl
webviewclient
echosign
onchange
python-imageio
fatfs
guice
windowsiot
cucumber-junit
microsoft-certifications
nppexec
cups
ftp-server
xll
netapp
distributed-caching
yoast
latexml
permission-denied
docker-ucp
kitura
realstudio
deployd
non-linear-regression
gtk#
word-2007
mpmovieplayercontroller
hmmlearn
cognos-tm1
gawk
ogg
visual-studio-monaco
coovachilli
piping
maximize
persistent-object-store
openmrs
vim-plugin
pack
odesk
gitlab-omnibus
wlanapi
embeddedwebserver
signed
python-c-extension
mutators
nanomsg
smacss
zend-mail
xhprof
object-code
libgcc
principalcontext
hyphen
colon
ax
acceptance-testing
semicolon
prototypal-inheritance
sigar
dynamic-c
qtextbrowser
server-variables
office-automation
directshow.net
eaccelerator
getimagesize
gui-designer
account-management

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