How to connect to Mongo database locally using python
Below are commands to create connection and query
from pymongo import MongoClient
client = MongoClient('hostname', 27017)
db = client.database_name
collection = db.collection_name
collection.find_one({"name":"name1"})
from pymongo import MongoClient
from bson import json_util
MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
DB_NAME = 'Your DB name'
COLLECTION_NAME = 'collectionname'
@app.route("/")
def getDatas():
connection = MongoClient(MONGODB_HOST, MONGODB_PORT)
collection = connection[DB_NAME][COLLECTION_NAME]
projects = collection.find()
json_projects = []
for project in projects:
json_projects.append(project)
json_projects = json.dumps(json_projects, default=json_util.default)
connection.close()
return json_projects
if __name__ == "__main__":
app.run(host='0.0.0.0',port=5000,debug=True)
Solution worked for me
#!pip install pymysql
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
#!pip install pymongo
import pymongo
import json
from pymongo import MongoClient
import pandas as pd
from pymongo import MongoClient
# if __name__ == '__main__':
# client = MongoClient("localhost", 27017, maxPoolSize=50)
client = MongoClient("localhost", 27017, maxPoolSize=50)
db = client.mydb
collection = db.angel
data = pd.DataFrame(list(collection.find()))
data
'''
Address City Name State ZIP _id
0 123 Main St Whereverville Jane Doe CA 90210 5af03bfa8ca570abf4a2f76c
1 555 Broadway Ave New York John Doe NY 10010 5af03bfa8ca570abf4a2f76d
'''