1

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" } ] }
2
  • please edit the formatting, this is horrible to read! I don't know about your problem, but maybe a write permissions problem? Can you try with a read request instead of a write request? Commented Jul 28, 2016 at 20:47
  • when I only do : db =get_db() print db it prints this: Database(MongoClient(host=['ds029735.mlab.com:29735'], document_class=dict, tz_aware=False, connect=True), u'myDB' which I guess means I am connected to db, but I can't seem to add anything Commented Jul 28, 2016 at 21:00

2 Answers 2

2
  1. 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).

  2. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for response. Why should user be Read-Only ? will check later if they work, but I know my user isn't Read-Only.
@VincFort it shouldn't but OperationFailure: not authorized on myDB to execute command can be thrown if it is.
0
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

1 Comment

add a description text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.