3.6 KiB
Writing to mongodb¶
Insure mongodb is actually installed on the system, The cell below creates a dataframe that will be stored within mongodb
#
# Writing to mongodb database
#
import transport
from transport import providers
import pandas as pd
_data = pd.DataFrame({"name":['James Bond','Steve Rogers','Steve Nyemba'],'age':[55,150,44]})
mgw = transport.get.writer(provider=providers.MONGODB,db='demo',collection='friends')
mgw.write(_data)
print (transport.__version__)
Reading from mongodb¶
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
- Basic read of the designated collection find=<collection>
- Executing an aggregate pipeline against a collection aggreate=<collection>
NOTE
By design read object are separated from write objects in order to avoid accidental writes to the database. Read objects are created with transport.get.reader whereas write objects are created with transport.get.writer
import transport
from transport import providers
mgr = transport.get.reader(provider=providers.MONGODB,db='foo',collection='friends')
_df = mgr.read()
PIPELINE = [{"$group":{"_id":0,"_counts":{"$sum":1}, "_mean":{"$avg":"$age"}}}]
_sdf = mgr.read(aggregate='friends',pipeline=PIPELINE)
print (_df)
print ('--------- STATISTICS ------------')
print (_sdf)
An auth-file is a file that contains database parameters used to access the database. For code in shared environments, we recommend
- Having the auth-file stored on disk
- and the location of the file is set to an environment variable.
To generate a template of the auth-file open the file generator wizard found at visit https://healthcareio.the-phi.com/data-transport