Karle Sleith
Karle Sleith

Reputation: 1

Print out Multiple Lines from MongoDB

Hey guys I'm running this code, and when I launch it up on "/d", I want it to print out the whole database, at the moment its only printing one line and changes to the next Object if I refresh the page, can you help?

from flask import Flask,render_template
import pymongo
from pymongo import MongoClient
client = MongoClient()
db = client.fliDb

app = Flask(__name__)

games = db.games
g= games.find()

@app.route('/d', methods = ["GET","POST"])
def content():
for game in g:
    str(game)
    return str(game)

Upvotes: 0

Views: 371

Answers (1)

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24007

You need to create a new PyMongo Cursor each time you request the page, instead of using a global cursor. Also, you need to completely iterate the cursor, instead of returning from inside the loop after the first iteration. Do this:

@app.route('/d', methods = ["GET","POST"])
def content():
    g = games.find()
    return '\n'.join([str(game) for game in g)])

Upvotes: 1

Related Questions