You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.6 KiB
Plaintext
129 lines
3.6 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Writing to mongodb\n",
|
|
"\n",
|
|
"Insure mongodb is actually installed on the system, The cell below creates a dataframe that will be stored within mongodb"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"2.0.4\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"#\n",
|
|
"# Writing to mongodb database\n",
|
|
"#\n",
|
|
"import transport\n",
|
|
"from transport import providers\n",
|
|
"import pandas as pd\n",
|
|
"_data = pd.DataFrame({\"name\":['James Bond','Steve Rogers','Steve Nyemba'],'age':[55,150,44]})\n",
|
|
"mgw = transport.get.writer(provider=providers.MONGODB,db='demo',collection='friends')\n",
|
|
"mgw.write(_data)\n",
|
|
"print (transport.__version__)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Reading from mongodb\n",
|
|
"\n",
|
|
"The cell below reads the data that has been written by the cell above and computes the average age within a mongodb pipeline. The code in the background executes an aggregation using **db.runCommand**\n",
|
|
"\n",
|
|
"- Basic read of the designated collection **find=\\<collection>**\n",
|
|
"- Executing an aggregate pipeline against a collection **aggreate=\\<collection>**\n",
|
|
"\n",
|
|
"**NOTE**\n",
|
|
"\n",
|
|
"By design **read** object are separated from **write** objects in order to avoid accidental writes to the database.\n",
|
|
"Read objects are created with **transport.get.reader** whereas write objects are created with **transport.get.writer**"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
" name age\n",
|
|
"0 James Bond 55\n",
|
|
"1 Steve Rogers 150\n",
|
|
"--------- STATISTICS ------------\n",
|
|
" _id _counts _mean\n",
|
|
"0 0 2 102.5\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"\n",
|
|
"import transport\n",
|
|
"from transport import providers\n",
|
|
"mgr = transport.get.reader(provider=providers.MONGODB,db='foo',collection='friends')\n",
|
|
"_df = mgr.read()\n",
|
|
"PIPELINE = [{\"$group\":{\"_id\":0,\"_counts\":{\"$sum\":1}, \"_mean\":{\"$avg\":\"$age\"}}}]\n",
|
|
"_sdf = mgr.read(aggregate='friends',pipeline=PIPELINE)\n",
|
|
"print (_df)\n",
|
|
"print ('--------- STATISTICS ------------')\n",
|
|
"print (_sdf)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"An **auth-file** is a file that contains database parameters used to access the database. \n",
|
|
"For code in shared environments, we recommend \n",
|
|
"\n",
|
|
"1. Having the **auth-file** stored on disk \n",
|
|
"2. and the location of the file is set to an environment variable.\n",
|
|
"\n",
|
|
"To generate a template of the **auth-file** open the **file generator wizard** found at visit https://healthcareio.the-phi.com/data-transport"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.9.7"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|