Calling MongoDB from Flask python3 on Ubuntu 22.04

79 views Asked by At

Following is the header of 'home.py' of a chat web app I am building using Flask:

from flask import Flask, render_template, url_for, request, redirect  
from kafka import KafkaConsumer
from kafka import KafkaProducer
import os
import time
import json
import pymongo

app = Flask(__name__)
app.secret_key = 'any random string'

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
user_db = myclient["authentication"]
user_table = user_db["user_info"]

Later the code has five lines:

print("0")
print(req)
print(type(user_table))
query = user_table.find({'uid':req['uid']})     
print(query)
print('1')

The output is:

0
{'uid': '', 'email': '', 'password': ''}
<class 'pymongo.collection.Collection'>
<pymongo.cursor.Cursor object at 0x7f62f684f700>
1

followed by flask error.

Kindly help.

I am stuck at 40 minutes 04 seconds of chapter 2 of this tutorial (https://www.youtube.com/playlist?list=PL5Tf33vPUXBJmeZmZsYH3Yyt51Rk6S7Pd).

1

There are 1 answers

1
Belly Buster On

.find() returns an iterable cursor, so either do:

query = user_table.find({'uid':req['uid']})     
for item in query:
    print(item)

or

query = user_table.find({'uid':req['uid']})     
print(list(query))