pymongo error

I seem to have a setup issue because I can run the amended code re the user_management test in jupyter notebook and the code runs as expected however when I run from cmd line/terminal test_invalid_user_should_not_have_preferences(client): fails with
AttributeError: ‘UpdateResult’ object has no attribute ‘get’ If I manually delete_user(‘’) in notebook it passes via the command line.
the other tests eg test_should_not_allow_None_as_prefs(client)
“pymongo.errors.OperationFailure: user is not allowed to do action [bypassDocumentValidation] on [sample_mflix.users]” Any pointers on where to look.

The suspect code
def update_prefs(email, prefs):
Given a user’s email and a dictionary of preferences, update that user’s
prefs = {} if prefs is None else prefs

    Ticket: User Preferences

    Update the "preferences" field in the corresponding user's document to
    reflect the information in prefs.

    # TODO: User preferences
    # Use the data in "prefs" to update the user's preferences.
    response = db.users.update_one(
        { "email": email },
        { "$set": { "preferences": prefs } }
    if response.matched_count == 0:
        return {'error': 'no user found'}
        return response
except Exception as e:
    return {'error': str(e)}

Hi @Jeff_Yenson

I’m not exactly certain what error you are encountering as it appears the code segment should be vaild for that test. If there is an issue with the notebooks but you are having no issues with the command line, I’d suggest using the CLI to avoid any further issues to completing the course.

Happy to help clarify any issues, but if you can elaborate on what the specific issue/issues you are having now then I’ll be better able to answer.

Kindest regards,

Thanks for the reply my issue is that the when running the test I get the error
OperationFailure: user is not allowed to do action [bypassDocumentValidation] on [sample_mflix.users]”. However when I copy the relevant code to the notebook eg from
code for
“def delete_user(email):”
“def update_prefs(email, prefs):”
“def get_user(email):”
“def add_user(name, email, hashedpw):”
“def login_user(email, jwt):”
“def logout_user(email):”
“def get_user_session(email):”
“def delete_user(email):”
copied into notebook with the relevant libraries imported. I can run the code from eg
def test_invalid_user_should_not_have_preferences(client):
# delete the test user if it exists
preferences = {
“color”: “green”,
“favorite_letter”: “q”,
“favorite_number”: 42

result = update_prefs('', preferences)

assert result.get('error', None) is not None
assert isinstance(result, UpdateResult) is False

with the correct results
print(result.get(‘error’, None),isinstance(result, UpdateResult))
but when I run from the terminal it fails with the error “pymongo.errors.OperationFailure: user is not allowed to do action [bypassDocumentValidation] on [sample_mflix.users]”

Hi @Jeff_Yenson

So I think the confusion here is that you shouldn’t be copy code from the notebooks to the file, the code within comments in the file is all that you need to edit. I think copying the code from the notebooks will probably introduce many errors and it’s not something we suggest or support.

In terms of the PyMongo Operational error, I’d suggest check that the whitelist in Atlas is valid for the IP your machine is using and that the user which you are connecting with has sufficient privileges in Atlas.

Hope this helps.

Kindest regards,

Ok thanks this is my whitelist

I’m not sure how my copying code from the into Notebook would introduce errors as it is just another way of checking the code.Which would be the same as me breaking up the into smaller parts as a .py file. I’ve even deleted the sample data from Atlas and reloaded it with the same result. And the user is the user that is setup in lesson one. And would not explain why I could complete the previous lessons.

This is my user access


Since m220student seems okay from your screenshot, I suspect the issue comes from your .ini. Please share it with us. Are you using virtualenv or anaconda? Post a screenshot of the whole terminal where you run the command line test.

I am using anaconda

Screenshot from terminal.

I tried running both when mflix was deactivated and activated

my ini file which is basically the same as the one in zip with the exception of the password and cluster address

The corresponding code in Notebook

I get the same result if run the code from a notebook terminal instance

Hi @Jeff_Yenson

If you run the “pytest -m connection” do these tests pass?
If they fail, can you try adding “?retryWrites=true&w=majority” to the end of both of the MFLIX_DB_URI strings in your .ini.
Can you repeat the “pytest -m connection” to see if it passes?

This will help better understand if there are any connection issues.
Can you also verify your cluster’s string as the “?retryWrites=true&w=majority” piece should have been present when you copied the string from Atlas so I wonder also if recopying the cluster string from Atlas isn’t also worth checking as a further test.

Kindest regards,

Hi Brazileoin
This is the string from Atlas to connect to compass

Without ?retryWrites=true&w=majority”

with ?retryWrites=true&w=majority”

test pytest -m user_preferences still fails

Hi @Jeff_Yenson

Can you share the full verbose output of the falling test by passing the “-v” option to pytest?

pytest -v -m user_preferences