I'm trying to insert a the result of a google search (using the Google custom search API) in the documents of my mongo collection. The search is performed on the value of the 'title'
field of each document.
I ran a test yesterday, but it appears that I misconfigured my API account, because I was still limited to 100 queries/day. In result, I have ~100 documents which 'google_search'
field contains the correct API result. All the others documents have a 'google_search'
field containing the following dict
:
{u'error': {u'code': 403,
u'errors': [{u'domain': u'usageLimits',
u'message': u'Daily Limit Exceeded',
u'reason': u'dailyLimitExceeded'}],
u'message': u'Daily Limit Exceeded'}}
I would thus like to perform a google_search on each document with a non-existent google_search
field, or on the ones containing the google_search.error
dict
I could do it that way:
for item in coll.find({'$or': [{'google_search': None},
{'google_search.error.code': {'$ne': None}}]):
coll.update({'_id': item['_id']}, {'$set': {'google_search': g_search(item['title'])}})
but i would like to know it there was any way to do the same thing with a single `coll.update(..., multi=True)
mongo call, instead of a for loop and several atomic update calls.
I feel that a single method call will be faster than a lengthy for loop, though I'm not sure I'm right.
Thank you very much!