0
votes

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??

1

1 Answers

2
votes

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().