Is there a way to store python objects directly in mongoDB without serializing them
There isn't a way to store an object in a file (database) without serializing it. If the data needs to move from one process to another process or to another server, it will need to be serialized in some form to be transmitted. Since you're asking about MongoDB, the data will absolutely be serialized in some form in order to be stored in the MongoDB database. When using MongoDB, it's BSON.
If you're actually asking about whether there would be a way to store a more raw form of a Python object in a MongoDB document, you can insert a Binary
field into a document which can contain any data you'd like. It's not directly queryable in any way in that form, so you're potentially loosing a lot of the benefits of using a NoSQL document database like MongoDB.
>>> from pymongo import MongoClient>>> client = MongoClient('localhost', 27017)>>> db = client['test-database']>>> coll = db.test_collection >>> # the collection is ready now >>> from bson.binary import Binary>>> import pickle>>> # create a sample object>>> myObj = {}>>> myObj['demo'] = 'Some demo data'>>> # convert it to the raw bytes>>> thebytes = pickle.dumps(myObj)>>> coll.insert({'bin-data': Binary(thebytes)})
Assuming you are not specifically interested in mongoDB, you are probably not looking for BSON. BSON is just a different serialization format compared to JSON, designed for more speed and space efficiency. On the other hand, pickle
does more of a direct encoding of python objects.
However, do your speed tests before you adopt pickle
to ensure it is better for your use case.
It seems you would still need to serialize using pickle
module that would create bytes and de-serializing these bytes with pickle
will directly provide python object.
Also, you can store pickled
object directly into Mongo.
import pickle as pklfrom uuid import uuid4from pymongo import MongoClientdata = dict(key='mongo')picked_data = pkl.dumps(data)uid = uuid4()client = MongoClient() # add DB url in the constructor if neededdb = client.test# insertiondb.data.insert_one({ 'uuid': uid, 'data': picked_data})# retrievalresult = db.data.find_one({'uuid': uid})assert pkl.loads(result['data']) == data