Skip to content

Instantly share code, notes, and snippets.

@Kasahs
Created January 28, 2016 08:28
Show Gist options
  • Save Kasahs/69c01cbbeaa27cf916f7 to your computer and use it in GitHub Desktop.
Save Kasahs/69c01cbbeaa27cf916f7 to your computer and use it in GitHub Desktop.
Some essential functions for connecting to mongodb, adding data to a collection etc. using pymongo.
from pymongo import MongoClient
def get_db():
try:
connection = MongoClient('localhost', 27019)
db = connection['db_name']
collection = db['collection_name']
return collection, connection
except Exception as e:
print(e.__str__())
return None
def save_document_to_db(document, collection):
collection.insert_one(document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment