Python pymongo not authorized for query - python

I am using pymongo to connect with mongodb, but I unable to access the data.
import pymongo
client = pymongo.MongoClient()
db = client['db_name']
for i in db.<collection>.find():
print(i)
Error.
pymongo.errors.OperationFailure: database error: not authorized for query on <collection>

Related

How can I import from db to json file to document db in mongo db?

I have a document db in amazon aws.I access this dbye with mongo shell.
I want to send the json file that I prepared to this document db from mylocal. How can I do that? How can I turn this mongo db out.
import json
import pymongo
# Making Connection
try:
myclient = pymongo.MongoClient("mongodb://xxx:zzz#/sample address")
print("Connected to MongoDB")
except Exception as e:
print(e)

pymongo.errors.OperationFailure: command insert requires authentication

I'm creating a collection and want to insert it in my database
I have imported pymongo and also I defined db = myClient["mydb"] this way but it says command insert requires authentication
>>> import pymongo
>>> from pymongo import MongoClient
>>> myClient = MongoClient()
>>> db = myClient.mydb
>>> users = db.users
>>> user1 = {"username": "nick", "password": "mysecurepass", "fav_num": 445}
>>> user_id = users.insert_one(user1).inserted_id
line 155, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: command insert requires authentication
It looks like the MongoDB instance you are using is set up with authentication, but when you create the connection using myClient = MongoClient() you are not giving it credentials. When you connect to the database try something like this:
client = MongoClient('example.com',
username='user',
password='password')
this will pass the correct username and password to the Mongo instance and allow you to connect. use this link for some examples on how to use authentication with pymongo.

"bad auth Authentication failed." error while connection to mongodb atlas in python

I am trying to connect to mongodb atlas using python using the following code:
import pymongo
myclient = pymongo.MongoClient("mongodb+srv://username:p%40ssword#cluster0-t9lf6.mongodb.net/test?retryWrites=true&w=majority")
mydb = myclient["test"]
mycol = mydb["somecol"]
dblist = myclient.list_database_names() #trial-1
x = mycol.find_one() #trail-2
I am getting error for both trail-1 and trail-2 as :
OperationFailure: bad auth Authentication failed.
Consider my username to be username and password to be p%40ssword with # replaced with %40. I checked my credentials, they are correct. But still i get this error. What to do ?

connecting to and using the same database in MongoDB python

I tried to connect to MongoDb server remotely using python pymongo, but when I tried to display documents from collection I got error message as
"pymongo.errors.OperationFailure: not authorized on pt to execute command { find: "devices", filter: {} }" .
Also when I tried get single record from mongo, it will not display the record details instead it will display as
"pymongo.cursor.Cursor object at 0x000001E883A14F98".
Mongo Server Details: Host: Someth-pt-ved-01
user: uname
pwd: mypass
authenticationDatabase: pt
collection: devices
My python code for connection is:
from pymongo import MongoClient
uri = "mongodb://uname:mypass#Someth-pt-ved-01:27017"
client = MongoClient(uri)
db = client.pt
collection = db.devices
#to get single record details
cursor = collection.find({'ID': 1490660})
print(cursor)
#to get all documents from collection-devices
for document in cursor:
print(document)
Note; I am working on Windows 10.

How to run raw mongodb commands from pymongo

In a mongo command line I can run
db.my_collection.stats()
I need to get my collections stats from Python so I tried
from pymongo import MongoClient
client = MongoClient()
db = client.test_database
collection = db.test_collection
collection.stats()
But I get
TypeError: 'Collection' object is not callable.
If you meant to call the 'stats' method on a 'Collection' object it is failing because no such method exists.
This is because pymongo does not support this method. How do I send raw mongoDB commands to mongo through Python?
from pymongo import MongoClient
client = MongoClient()
db = client.test_database
print(db.command("collstats", "test_collection"))
Approach 1 with PyMongo:
client = pymongo.MongoClient(host = "127.0.0.1", port = 27017)
db = client.test_database
db.command("dbstats") # prints database stats for "test_db"
db.command("collstats", "test_collection") # prints collection-level stats
This can be done with this approach in Django.
from django.db import connections
database_wrapper = connections['my_db_alias']
eggs_collection = database_wrapper.get_collection('eggs')
eggs_collection.find_and_modify(...)
From django-mongodb-engine docs:
django.db.connections is a dictionary-like object that holds all
database connections – that is, for MongoDB databases,
django_mongodb_engine.base.DatabaseWrapper instances.
These instances can be used to get the PyMongo-level Connection,
Database and Collection objects.

Categories