Advanced Data Analytics Using Python_ With Machine Learning, Deep Learning and NLP Examples ( 2023)
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 2
ETL with Python (Structured Data)
To import data into a MongoDB instance running on a different host
or port, the hostname or port needs to be mentioned specifically in the
mongoimport command by including the --host or --port option.
There is a similar load command in MySQL.
Create a Connection Using pymongo
To create a connection, do the following:
import MongoClient from pymongo.
Client11 = MongoClient()
If no argument is mentioned to MongoClient, then it will default to the
MongoDB instance running on the localhost interface on port 27017.
A complete MongoDB URL may be designated to define the
connection, which includes the host and port number. For instance, the
following code makes a connection to a MongoDB instance that runs on
mongodb0.example.net and the port of 27017:
Client11 = MongoClient("mongodb://myhostname:27017")
Access Database Objects
To assign the database named primer to the local variable DB, you can use
either of the following lines:
Db11 = client11.primer
db11 = client11['primer']
Collection objects can be accessed directly by using the dictionary
style or the attribute access from a database object, as shown in the
following two examples:
Coll11 = db11.dataset
coll = db11['dataset']
37