Tuesday 23 May 2017

Mongodb changing contents of saved binary data

I'm trying to save and retrieve a python pickle from MongoDB in a nodeJS app. Using mongoose, I save the pickle like this

var Model = new Model()
model.pickle = fs.readFileSync(picklePath)
model.save(function(err){...})

Later, I then read the pickle and write it to disk like so

Model.find({id: ...}, (err, data) => {
    fs.writeFileSync('data.pkl', data.pickle)
})

The problem is that in the newly written pickle some of the characters have changed. Manually inspecting both files I can see that mongo seems to have changed the original content with � character.

Original pickle contents

?cpandas.core.frame
DataFrame
q)?qcpandas.core.internals
BlockManager
q)?q(]q(cpandas.core.index

New pickle contents �cpandas.core.frame DataFrame q)�qcpandas.core.internals BlockManager

I have no idea on whats going on the with encoding.



via KingKongi

No comments:

Post a Comment