Question

I have a mongodb collection, and I have to upsert some data into it, based upon whether a subdocument is present inside the document or not. A sample document document inside the collection is

{"follow_request_sent": "null", 
        "profile_use_background_image": "true", 
        "default_profile_image": "false", 
        "id": 87174680, 
        "verified": "false", 
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/378800000220301249/a0c7b8c5766de83b65a42ca52196c4b3_normal.jpeg", 
        "profile_sidebar_fill_color": "EADEAA", 
        "profile_text_color": "333333", 
        "followers_count": 348, 
        "profile_sidebar_border_color": "D9B17E", 
        "id_str": "87174680", 
        "profile_background_color": "8B542B", 
        "listed_count": 5, 
        "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/350203578/Photo0003.jpg", 
        "utc_offset": 19800, 
        "statuses_count": 20119, 
        "description": "Sports Lover", 
        "friends_count": 708, 
        "location": "India", 
        "profile_link_color": "9D582E", 
        "profile_image_url": "http://pbs.twimg.com/profile_images/378800000220301249/a0c7b8c5766de83b65a42ca52196c4b3_normal.jpeg", 
        "following": "null", 
        "geo_enabled": "true", 
        "profile_background_image_url": "http://a0.twimg.com/profile_background_images/350203578/Photo0003.jpg", 
        "name": "Ronak Baj", 
        "lang": "en", 
        "profile_background_tile": "true", 
        "favourites_count": 17, 
        "screen_name": "ronakbaj", 
        "notifications": "null", 
        "url": "null", 
        "created_at": "Tue Nov 03 12:02:56 +0000 2009", 
        "contributors_enabled": "false", 
        "time_zone": "New Delhi", 
        "protected": "false", 
        "default_profile": "false", 
        "is_translator": "false"
    }, 

I have to update this based on the value of id, such that if id is already in document, update it, else create new document. The syntax I have used for this is:

posts4 = db4.posts
post_id4 = posts4.update(posts4.find_one({'id' : usr.get('id')}), dict4, upsert = True)

dict4 is the new document to be updated, if id is found

However, this is giving me an error, the complete traceback for this is:

  File "new.py", line 63, in <module>
    stream.statuses.filter(follow = 95995660)
  File "/usr/lib/python2.7/site-packages/twython/streaming/types.py", line 65, in filter
    self.streamer._request(url, 'POST', params=params)
  File "/usr/lib/python2.7/site-packages/twython/streaming/api.py", line 148, in _request
    if self.on_success(data):  # pragma: no cover
  File "new.py", line 48, in on_success
    post_id4 = posts4.update(posts4.find_one({'id' : usr.get('id')}), dict4, upsert = True)
  File "/usr/lib64/python2.7/site-packages/pymongo/collection.py", line 463, in update
    raise TypeError("spec must be an instance of dict")
TypeError: spec must be an instance of dict

Please help!!

Was it helpful?

Solution

The search criteria to update must be a dictionary when passed as update argument. You can directly pass the dictionary to update as below:

posts4 = db4.posts
post_id4 = posts4.update({'id' : usr.get('id')}, dict4, upsert = True)

You can check update in mongodb for details

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top