6

Say I have a json object in variable somejson (a dictionary). I have a myjson object declared like this:

myjson['a'] = 'valueofa'
myjson['b'] = 'valueofb'
myjson['c'] = somejson

I want to take this myjson object and insert it into mongodb. How do I accomplish this? I read that mongoengine is preferred over pymongo, but everything I see on mongoengine appears to have great emphasis on documents and nothing to do with existing json. Please shed some light through some sample code or shed some light on what is the best way to insert my myjson object into mongodb, and be able to retrieve it. Is mongoengine the best way to accomplish what I am looking for? Or is it pymongo that is better for this case?

I think it should be very simple, but have not found anything that really does a good job explaining what has to be done.

I have looked at the following tutorial: http://docs.mongodb.org/manual/tutorial/write-a-tumblelog-application-with-flask-mongoengine/

Under the Define the Schema section, it talks about a lot of document heavy information to prepare the structure, but nothing about getting existing an existing JSON structure into it.

1 Answer 1

6

If you want to use mongoengine, you can store your dictionary in DictField, for example, or unpack it to DynamicDocument. And you always will be able to retrieve it.

In pymongo your can just store your dict to document without schema, in your case because bson supports all json types. And it will be like subdocument in document.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.