I have a remote MongoDB with scraped data that I want to display via webpage in Flask, but it seems to be running into issues. I'm able to add to the DB without issue, but displaying data from the DB seems like an impossible feat. I'm at a loss after repeated research. One common error is that the 'Cursor' object is not callable
Code:
from flask import Flask, render_template
from flask_pymongo import PyMongo
app = Flask(__name__)
app.config["MONGO_URI"] = 'mongodb+srv://example:example@cluster0-zh34t.mongodb.net/test?retryWrites=true'
mongo = PyMongo(app)
@app.route("/")
def index():
doc = mongo.db.collection.find_one({"_id": 0})
return doc
Cursor
was not the real issue here. Using find_one
instead of find
passes MongoDB into a dictionary that you can then use as expected. My issue, which is now resolved, was due to the MONGO_URI
specified. Because of how flask_pymongo
automatically identifies your DB based on the URI, I had "Test" as opposed to my actual DB. "Test" didn't exist, although MongoDB Atlas provided the path, so I ran into all kinds of issues. If you experience this type of issue, be sure to triple-check your URI.