1

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
not 0x12
  • 19,360
  • 22
  • 67
  • 133
plaintoast
  • 59
  • 6

1 Answers1

2

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.

plaintoast
  • 59
  • 6
  • 1
    I can share this experience that most of the mistakes are within the URI. Make sure you understand https://docs.mongodb.com/manual/reference/connection-string/ and in particular don't overlook the options part like `authSource=authDB` which is necessary if you use authentication. – B--rian Mar 19 '19 at 21:36