1

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!

Balthazar Rouberol
  • 6,822
  • 2
  • 35
  • 41

1 Answers1

0

Ok. Forget my post. Found this. You cannot reference the current document's properties from within an update(). You'll have to iterate through all the documents and update them.

So no one huge update here.

It is ok to do such things in a loop.

Community
  • 1
  • 1
alexvassel
  • 10,600
  • 2
  • 29
  • 31
  • I got a trackeback stating that ``item`` is not defined – Balthazar Rouberol Nov 29 '12 at 10:31
  • More precisely, here is the code + traceback: >>> act.update(query, {'$set': {'google_search': g_search(item['title'])}}, multi=True) NameError Traceback (most recent call last) in () ----> 1 act.update(query, {'$set': {'google_search': g_search(item['title'])}}, multi=True) NameError: name 'item' is not defined – Balthazar Rouberol Nov 29 '12 at 10:55