Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Mongo database locally using python

I am using ipython 2.7. I am creating database name enron in mongodb. I have tried to connect to this database locally but the following error occurred - how do I fix it?

this my code:

import json
import pymongo  # pip install pymongo
from bson import json_util 
from pymongo import MongoClient# Comes with pymongo
conn = pymongo.Connection('mongodb://user:[email protected]:33499/enron')
client = MongoClient()

error:

ConnectionFailure: could not connect to localhost:27017: [Errno 10061] No connection     could be made because the target machine actively refused it
like image 305
pramod24 Avatar asked Mar 03 '14 05:03

pramod24


People also ask

How do I connect to a local database in MongoDB?

To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.

Can we connect MongoDB with Python?

MongoDB is a document-oriented database classified as NoSQL. It's become popular throughout the industry in recent years and integrates extremely well with Python. Unlike traditional SQL RDBMSs, MongoDB uses collections of documents instead of tables of rows to organize and store data.

What is MongoDB localhost URL?

Without any arguments, the mongo command attempts to connect to a local MongoDB instance. To do this, it attempts to connect to port 27017 on the local loopback address: 127.0. 0.1:27017 .


1 Answers

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"})
like image 158
Amit Avatar answered Sep 19 '22 11:09

Amit