Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8833555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:48:27+00:00 2026-06-14T08:48:27+00:00

I’m trying to write a defaultdict variable to a document in my MongoDB. Everything

  • 0

I’m trying to write a defaultdict variable to a document in my MongoDB. Everything else sets fine, just not this one attribute, its bizarre! I’m setting a rather large defaultdict called ‘domains’, which has worked many times before. Check out this terminal output:

So here’s my defaultdict:

>>> type(domains) 
<type 'collections.defaultdict'>

Its pretty big, about 3mb:

>>> sys.getsizeof(domains)
3146008

Here’s the document we’ll set it to:

>>> db.AggregateResults.find_one({'date':'20110409'}).keys()
[u'res', u'date', u'_id']

Let’s grab that document’s ID:

>>> myID = db.AggregateResults.find_one({'date':'20110409'})['_id']
>>> myID
ObjectId('50870847f49a00509a000000')

Great, let’s set the attribute:

>>> db.AggregateResults.update({'_id':myID}, {"$set": {'domains':domains}})
>>> db.AggregateResults.find_one({'date':'20110409'}).keys()
[u'res', u'date', u'_id']

EH? It didn’t save??

Hmmm…does anything save at all?

>>> db.AggregateResults.update({'_id':myID}, {"$set": {'myTest':'hello world'}})
>>> db.AggregateResults.find_one({'date':'20110409'}).keys()
[u'myTest', u'res', u'date', u'_id']

Okay…so it can save things fine…perhaps its because MongoDB doesn’t like defaultdicts? Let’s try:

>>> myDD = defaultdict(int)
>>> myDD['test'] = 1
>>> myDD
defaultdict(<type 'int'>, {'test': 1})
>>> db.AggregateResults.update({'_id':myID}, {"$set": {'myDD':myDD}})
>>> db.AggregateResults.find_one({'date':'20110409'}).keys()
[u'myTest', u'res', u'date', u'myDD', u'_id']

So it can save defaultdicts fine, just not this one??

So strange! Any ideas why??

EDIT with safe=True:

>>> db.AggregateResults.update({'_id':myID}, {"$set": {'domains':domains}}, safe=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/site-packages/pymongo-2.1.1_-py2.6-linux-x86_64.egg/pymongo/collection.py", line 405, in update
    _check_keys, self.__uuid_subtype), safe)
  File "/usr/lib64/python2.6/site-packages/pymongo-2.1.1_-py2.6-linux-x86_64.egg/pymongo/connection.py", line 796, in _send_message
    return self.__check_response_to_last_error(response)
  File "/usr/lib64/python2.6/site-packages/pymongo-2.1.1_-py2.6-linux-x86_64.egg/pymongo/connection.py", line 746, in __check_response_to_last_error
    raise OperationFailure(error["err"], error["code"])
pymongo.errors.OperationFailure: not okForStorage

This GoogleGroup discussion says that could be due to having fullstops in the keys, but:

>>> [x for x in domains.keys() if '.' in x]
[]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:48:28+00:00Added an answer on June 14, 2026 at 8:48 am

    Aha! Found it!

    Not only can keys in MongoDB not have ‘.’, they also cannot have ‘$’ in them.

    See:

    >>>[x for x in domains.keys() if '$' in x]
    ['$some_key_']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.