Reputation: 1180
this is my first shot at using dbs and I'm having some trouble with the basics. Tried to look online but couldnt find answers to simple questions. When I try to add some info to my db, I get a whole bunch of errors.
import pymongo
def get_db():
from pymongo import MongoClient
client = MongoClient("mongodb://xxxxxx:[email protected]:29735/xxxxxxx")
db = client.myDB
return db
def add_country(db):
db.countries.insert({"name": "Canada"})
def get_country(db):
return db.contries.find_one()
db = get_db()
add_country(db)
I got this error message:
File "/Users/vincentfortin/Desktop/Python_code/mongo.py", line 21, in <module>
add_country(db)
File "/Users/vincentfortin/Desktop/Python_code/mongo.py", line 11, in add_country
db.countries.insert({"name": "Canada"})
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py", line 2212, in insert
check_keys, manipulate, write_concern)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py", line 535, in _insert
check_keys, manipulate, write_concern, op_id, bypass_doc_val)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/collection.py", line 516, in _insert_one
check_keys=check_keys)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/pool.py", line 239, in command
read_concern)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/network.py", line 102, in command
helpers._check_command_response(response_doc, None, allowable_errors)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pymongo/helpers.py", line 205, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: not authorized on myDB to execute command { insert: "countries", ordered: true, documents: [ { _id: ObjectId('579a6c6ed51bef1274162ff4'), name: "Canada" } ] }
Upvotes: 1
Views: 2277
Reputation: 1
from pymongo import MongoClient
#import json
client = MongoClient('localhost', 27017)
mydb = client.db_University
def add_client(student_name, student_age, student_roll, student_branch):
unique_client = mydb.students.find_one({"name":student_name}, {"_id":0})
if unique_client:
return " already exists"
else:
mydb.students.insert(
{
"name" : student_name,
"age" : student_age,
"roll no" : student_roll,
"branch" : student_branch,
})
return "student added successfully"
student_name = raw_input("Enter stuent Name: ")
student_age = raw_input("Enter student age: ")
student_roll = raw_input("Enter student roll no: ")
student_branch = raw_input("Enter student branch: ")
print add_client(student_name,student_age,student_roll,student_branch)
def view_client(student_name):
user = mydb.students.find_one({"name":student_name}, {"_id":0})
if user:
name = user["name"]
age = user["age"]
rollno = user["roll no"]
branch = user["branch"]
return {"name":name,"age":age,"roll no":rollno,"branch":branch}
else:
return "Sorry, No such student exists"
user = raw_input("Enter student name to find: ")
user_data = view_client(user)
print user_data
Upvotes: 0
Reputation: 4235
Check twice if your xxxxxxx
from ds029735.mlab.com:29735/xxxxxxx
is equal to myDB
from db = client.myDB
. I mean if your connection string is mongodb://username:[email protected]:29735/xyz
then your code should be db = client.xyz
and not db = client.zyx
(or other names).
Check in mLab control panel if your user is Read-Only https://i.sstatic.net/UIRr2.png
Both of these issues returns errors like your so I don't know with which one you have faced.
Upvotes: 2