nlper
nlper

Reputation: 2407

storing object value in mongodb

I am getting some object values in python which I want to store in mongodb for later use, but with no luck

def callPylink(self):
    from pylinkgrammar.linkgrammar import Parser
    import sys
    p = Parser()
    linkage = p.parse_sent("When was Gandhi born")
    try:
        print "Linkage 0 ", linkage[0]
    except:
        print "error"
    self.collection.insert(linkage[0].__dict__)

at insert line:

Linkage 0 <pylinkgrammar.linkgrammar.Linkage object at 0x92db84c>

Traceback (most recent call last):
  File "objmongo.py", line 28, in <module>
    data = obj.callPylink()
  File "objmongo.py", line 23, in callPylink
    self.collection.insert(linkage[0].__dict__)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 409, in insert
    gen(), check_keys, self.uuid_subtype, client)
bson.errors.InvalidDocument: Cannot encode object: <S: When, was>

what is other way to store python object in mongo.

update

    pickled = pickle.dumps(linkage[0])
    self.collection.insert(pickled)

gives error:

  File "objmongo.py", line 30, in <module>
    data = obj.callPylink()
  File "objmongo.py", line 25, in callPylink
    self.collection.insert(pickled)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 409, in insert
    gen(), check_keys, self.uuid_subtype, client)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/collection.py", line 386, in gen
    doc['_id'] = ObjectId()
TypeError: 'str' object does not support item assignment

Upvotes: 4

Views: 3410

Answers (1)

To store a dictionary in mongodb, your dictionary can only contain string keys and those types of values that mongodb can serialize (encode). This is not the case here.

You can serialize complex object trees into byte strings and deserialize them back using the pickle module, use pickle.dumps(obj) to serialize an object into a bytestring, and pickle.loads(bytestring) to deserialize the object

Example: given the following class and instance

>>> class OurClass():
...     def __init__(self, arg):
...         self.arg = arg
... 
>>> instance = OurClass(42)

we import the pickle module

>>> import pickle
>>> pickled = pickle.dumps(y)
>>> pickled
b'\x80\x03c__main__\nX\nq\x00)\x81q\x01}q\x02X\x03\x00\x00\x00argq\x03K*sb.'

the pickled is the value to store into database in a field of a document, like:

my_doc = { 'linkage': pickled }
self.collection.insert(my_doc)

Now you can restore the original value with pickle.loads

>>> restored = pickle.loads(pickled)
>>> restored
<__main__.OurClass object at 0x7fc91cb35cf8>
>>> restored.arg
42

Upvotes: 4

Related Questions