pymongo 3.4.0 connecting to mongo atlas - mongodb

I have python 2.7, have pymongo 3.4
and im trying to connect to mongo atlas using the following :
import certifi
import ssl
import os
from pymongo import MongoClient
MongoClient(
"mongodb://Admin:<PASSWORD>#ABC-shard-00-00-XYZ:27017",
"ABC-shard-00-01-XYZ:27017",
"ABC-shard-00-02-XYZ:27017/dev?ssl=true&replicaSet=ABC-0&authSource=admin",
ssl_cert_reqs=ssl.CERT_REQUIRED,
ssl_ca_certs=certifi.where()
)
it keeps failing with Authentication failed. code 18

As described in Using PyMongo with MongoDB Atlas:
Since PyMongo v3.4.0, you can connect to MongoDB Atlas by passing the Connection String URI provided by MongoDB Atlas to MongoClient, example:
from pymongo import MongoClient
client = MongoClient("mongodb://USER:PASSWORD#ABC-cluster-shard-00-00-XYZ.mongodb.net:27017,ABC-cluster-shard-00-01-XYZ.mongodb.net:27017,ABC-cluster-shard-00-02-XYZ.mongodb.net:27017/DATABASE?ssl=true&replicaSet=ABC-cluster-shard-0&authSource=admin")
Connections to MongoDB Atlas require TLS/SSL. With PyMongo 3.3+, you can install PyMongo 3.3+ and any TLS/SSL-related dependencies using the following Python pip command:
python -m pip install pymongo[tls]

Related

Import data from MondoDB localhost to MongoDB Atlas

I have this database in MongoDB Compass interface which is stored (not sure if this is the right way to describe it) in localhost:
enter image description here
Is there a way to export the data from there and import it into MongoDB cloud version?
Thank you!
first connect mongodb compass to local db and exports data , then connect mongodb compass to mongo atlas and import exported data

flask_pymongo : PyMongo vs MongoClient : [SSL: CERTIFICATE_VERIFY_FAILED]

I have a huge flask application and I use the PyMongo class from flask_pymongo for MongoDB operations. My issue is in development environment.
I have a MONGO_URI in my config.py like this:
MONGO_URI = "mongodb+srv://username:password#cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority"
Usage in my app looks like this:
# This is how I have initialized it in '__init__.py'
from flask_pymongo import PyMongo
mongo = PyMongo(app)
# This is how I access collections in the specified DB
document = mongo.db[collection_name].find() # Throws the below error
This worked fine until a couple days ago when I reinstalled Windows and also Python from scratch.
Now the same code throws the following error:
pymongo.errors.ServerSelectionTimeoutError: cluster-name-shard-00-01.pihvl.gcp.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
But using MongoClient instead of PyMongo works just fine, like this:
# Using MongoClient and setting 'ssl_cer_reqs'
from flask_pymongo import MongoClient
import ssl
mongo = MongoClient(app.config['MONGO_URI'], ssl_cert_reqs=ssl.CERT_NONE)
# This works just fine
document = mongo[db_name][collection_name].find()
Questions
What changed with PyMongo when I reinstalled everything? I don't want to use MongoClient simply for the fact that there are over 100 endpoints and it is a nightmare to shift now, and also I prefer PyMongo because I don't have to specify the db_name in every query, it gets that from the MONGO_URI.
What am I missing and how do I make it work with PyMongo? Is there a way to set the ssl_cert_reqs while using PyMongo?
Something in the certificate chain will have changed. pymongo has specific documentation on how to troubleshoot this issue:
https://pymongo.readthedocs.io/en/stable/examples/tls.html#troubleshooting-tls-errors
IMPORTANT: Do this only if you're facing this issue in development environment
All I had to do was add &ssl=true&ssl_cert_reqs=CERT_NONE to my MONGO_URI in my Development Configuration so now my uri would change from:
mongodb+srv://username:password#cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority
to:
mongodb+srv://username:password#cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE
pip install certifi
import pymongo
import certifi
myclient = pymongo.MongoClient(CONNECTION_STRING, tlsCAFile=certifi.where())
...

Querying MongoDB from browser using a flask backend

I am building an interactive visualization tool that lets users query a database which is then visualized using D3, Flask as the server and MongoDB as the database. My question: How do I query the MongoDB (from Flask) with the user input and render this to the server?
Install the mongodb and run its server (in terminal type mongo)
Install pymongo (python package)
Then create an instance of the pymong using:
from pymongo import MongoClient
client = MongoClient(MONGO_URL)
db = getattr(client, DATABASE_NAME)
4. Then you can query using following:
> documents = db.your_collection.find()
> db.your_collection.insert({'name': 'Nabin Khadka'})
These python code can be wrapped in views.py file under a method. Like:
#app.route('/test')
def test():
# All above code
return jsonify(response_dictionary)
Then running the app, we can call the following url from browser:
https://url_to_server:port/test

Connection to pymongo

I am trying to connect to mongo in MAC using pymongo. I am getting the following error-
>>> from pymongo import MongoClient
Traceback (most recent call last):
File "", line 1, in
from pymongo import MongoClient
ImportError: cannot import name 'MongoClient'
I have tried Connection also. But it gives the same error. Any help?
Steps for troubleshooting:
First of all, verify if your environment is activated and you are in the correct environment.
If it is active and you're in the correct environment, then verify if you have installed pymongo.
If it's not installed in your environment, install it using pip install pymongo in the environment you want to work.

MongoDB w/ PyMongo on Heroku -- error: no module named pymongo

I am making a heroku app using Flask with a mongoDB backend. I am using pymongo as my driver but when I push my code to git, it crashes and spits out this error:
2014-04-05T09:56:36.301695+00:00 app[web.1]: ImportError: No module named pymongo
The web-app works locally and pymongo works completely fine in that scenario. What do I have to do to have pymongo recognized on heroku's servers?
Thansks!
You need to make Heroku aware of PyMongo as a requirement. The easiest way to do this is my adding pymongo==x.x to your requirements.txt file.
If you are using a setup.py script instead of requirements.txt, add 'pymong==x.x', to the install_requires argument to setup.
In both cases, make sure to replace x.x with the appropriate version number.