Wiki source code of How to Use Pymongo to Store Data
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | First, [[set up a virtualenv>>doc:Main.How to Create a VirtualEnv.WebHome]]. | ||
| 2 | |||
| 3 | Make sure that your [[mongo server is running>>url:https://wiki.codepowered.com/wiki/How_to_connect_to_mongo_server]]. | ||
| 4 | |||
| 5 | [[In the virtualenv, install pymongo>>url:https://wiki.codepowered.com/wiki/How_to_add_libraries_to_a_virtualenv]], then try out this .py file [[in the virtualenv>>url:https://wiki.codepowered.com/wiki/How_to_Run_a_Python_Script_in_a_VirtualEnv]]. | ||
| 6 | |||
| 7 | {{code language="python"}} | ||
| 8 | from pymongo import MongoClient | ||
| 9 | client = MongoClient() | ||
| 10 | db = client['test-database'] | ||
| 11 | collection = db['test-collection'] | ||
| 12 | print(collection.insert_one({'my_key': 'some value'})) | ||
| 13 | {{/code}} | ||
| 14 | |||
| 15 | The printout shows the ID assigned by mongodb to the data you stored. You can now try to [[look it up>>url:https://wiki.codepowered.com/wiki/How_to_Use_Pymongo_and_Search_for_Data]]. | ||
| 16 | |||
| 17 | You can also follow [[tutorial>>url:https://api.mongodb.com/python/current/tutorial.html%7Cthis]]. |