Movies.find() and movies.find_one returning empty

I have all the data there in the mflix database, as I see in mongo atlas. I’ve tried the user account we set up at first. I’ve added another user with readWrite on all data bases, but I wonder if there’s some way other than this task that I can check that user’s access.
Tried both users in the uri object in the template syntax. The m220user comes back with unauthorised access. The original m220student comes with an empty document.
I’ve trawled net for sources. This one https://docs.atlas.mongodb.com/driver-connection/#python-driver-example speaks of connecting to a cluster as opposed to a database in the SRV, but when I seek out examples and the automatic string generated at Atlas it always comes back to the original.
I’m wondering if the public API access key I generated in the earlier workshops need be applied here.
This is output from my two latest attempts. (Sorry that there’s a syntax error in my last movies.find() command, which I had to run again.)

λ python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type “help”, “copyright”, “credits” or “license” for more information.

import pymongo
uri=“mongodb+srv://m220user:m220password@mflix-cd2dr.mongodb.net/test?retryWrites=true”
client=pymongo.MongoClient(uri)
m220=client.m220
movies=m220.movies
movies.find_one()
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\brian\Anaconda3\lib\site-packages\pymongo\collection.py”, line 1262, in find_one
for result in cursor.limit(-1):
File “C:\Users\brian\Anaconda3\lib\site-packages\pymongo\cursor.py”, line 1189, in next
if len(self.__data) or self._refresh():
File “C:\Users\brian\Anaconda3\lib\site-packages\pymongo\cursor.py”, line 1104, in _refresh
self.__send_message(q)
File “C:\Users\brian\Anaconda3\lib\site-packages\pymongo\cursor.py”, line 982, in __send_message
helpers._check_command_response(first)
File “C:\Users\brian\Anaconda3\lib\site-packages\pymongo\helpers.py”, line 155, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: user is not allowed to do action [find] on [m220.movies]

uri=“mongodb+srv://m220student:m220password@mflix-cd2dr.mongodb.net/test?retryWrites=true”
client=pymongo.MongoClient(uri)
m220=client.m220
movies=m220.movies
movies.find_one()
from bson.json_util import dumps
cursor=movies.find(“cast”:“Salma Hayek”},{“title”:1, “_id”:0})
File “”, line 1
cursor=movies.find(“cast”:“Salma Hayek”},{“title”:1, “_id”:0})
^
SyntaxError: invalid syntax

cursor=movies.find({“cast”:“Salma Hayek”},{“title”:1, “_id”:0})
print(dumps(cursor, indent=2))

Hi @Brian_18814,

Please try using mflix database. I connected using your connection string:

 mongo mongodb+srv://m220user:m220password@mflix-cd2dr.mongodb.net/test

> use mflix
> db.movies.find({"cast":"Salma Hayek"},{title:1, _id:0})

Let me know if it works.

Kanika

This works Kanik. But this isn’t an import from Mongodb is it? Don’t I need to be importing the data to my host environment?

the syntax error was occurring because you are missing an opening { for the cast dict.

Thanks Kanik, but notice I ran the corrected command again and I have nothing in response to movies.find_one(). I’m going to try creating the user within the mongo shell as opposed to relying on Atlas front end.

I tried db.createUser in mongo at the mflix database but wasn’t permitted. Maybe I should create a root super user. At a loss just now.

I think you should try to create the user for your cluster directly in Atlas, not via the mongo shell.

Mongod only allows the creation of the first user only when connected vi localhost. On your cluster you are definitively not from localhost.

Hi Brian,

Why dont you give Jupyter a try? Sorry, I really dont understand what you are trying to achieve as I am new to mongoDB and stuff.

Thanks,
Shahul

THanks mjshaheed. When you say Jupyter, I run Jupyter notebook from my cmdline to open the notebook guides in a html app with a glossary of sorts for mongo client commands. I hope I worded that correctly.

OK Thanks. I am just learning. I think you can change username/password and try running one by one?

I’ve created two users on Mongo Atlas front end. m220student was admin only as I saw it and added readWrite privileges when I had this problem m220user has what I created as a userAdvanced role which was to readWrite all databases and something else I think. Both these seem to be authorised on the admin database. I presume that’s ok and won’t preclude from accessing mflix database.
It seems that I simply can’t access the mflix database from the pymongo client with the given srv. I tried to cobble together all cluster names instead of the data base only but couldn’t bring that off. Maybe I should try that again. Have tried everything I can think of up to this point.

If you’re passing by this chat mjshaheed, I got my movies data. I put the mflix (db) into the uri instead of test and that makes it the current db I believe. At least that’s how it works if I mongo the uri. So the uri (SRV in mongo terms) I use is:
uri=“mongodb+srv://m220student:m220password@mflix-cd2dr.mongodb.net/mflix”
I assign a client variable then an mflixdb variable in that order and I can extract data from there. Here’s my code, which you can compare with the advised code and my original. It includes output. As you see, you get only one pass of the cursor object before it becomes redundant.
C:\cmderMini\cmder_mini
λ python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type “help”, “copyright”, “credits” or “license” for more information.

import pymongo
uri=“mongodb+srv://m220student:m220password@mflix-cd2dr.mongodb.net/mflix”
client=pymongo.MongoClient(uri)
mflixdb=client.mflix
cursor=mflixdb.movies.find({“cast”:“Salma Hayek”},{“title”:1,"_id":0})
from bson.json_util import dumps
print(dumps(cursor))
[{“title”: “Roadracers”}, {“title”: “Midaq Alley”}, {“title”: “Desperado”}, {“title”: “Breaking Up”}, {“title”: “Fools Rush
In”}, {“title”: “The Hunchback”}, {“title”: 54}, {“title”: “Frida”}, {“title”: “The Velocity of Gary”}, {“title”: “Wild Wil
West”}, {“title”: “No One Writes to the Colonel”}, {“title”: 54}, {“title”: “Chain of Fools”}, {“title”: “In the Time of t
e Butterflies”}, {“title”: “Once Upon a Time in Mexico”}, {“title”: “After the Sunset”}, {“title”: “Ask the Dust”}, {"title
: “Bandidas”}, {“title”: “Lonely Hearts”}, {“title”: “Puss in Boots”}, {“title”: “The Prophet”}, {“title”: “Here Comes the
oom”}, {“title”: “Sausage Party”}, {“title”: “Some Kind Of Beautiful”}, {“title”: “Here Comes the Boom”}, {“title”: “As Luc
Would Have It”}, {“title”: “Everly”}, {“title”: “Tale of Tales”}, {“title”: “Septembers of Shiraz”}]

print(dumps(cursor, indent=1))

the problem was from the instructor in the video. there is no m220 database but mflix is the correct database

4 Likes

Hi Chika. I am too have the same problem with any find({}) returning empty lists.
I dint quite understand how it was solved above. Can you help out?

after client=… line
change m220=client.m220 to m220=client.mflix
this is so because the data given to us dosent have m22o collection but only mflix collection

1 Like

Thanks Chika. I just realized as i was going through the code. Thank you so much for helping me.

1 Like

your welcome. cheers

1 Like