使用Flask后端从浏览器查询MongoDB [英] Querying MongoDB from browser using a flask backend
本文介绍了使用Flask后端从浏览器查询MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个交互式的可视化工具,该工具使用户可以查询数据库,然后使用D3,Flask作为服务器以及MongoDB作为数据库对其进行可视化.我的问题:如何使用用户输入查询MongoDB(从Flask中获取)并将其呈现给服务器?
I am building an interactive visualization tool that lets users query a database which is then visualized using D3, Flask as the server and MongoDB as the database. My question: How do I query the MongoDB (from Flask) with the user input and render this to the server?
推荐答案
- 安装mongodb并运行其服务器(在终端类型mongo中)
- 安装pymongo(python软件包)
- 然后使用以下方法创建pymong的实例:
from pymongo import MongoClient
client = MongoClient(MONGO_URL)
db = getattr(client, DATABASE_NAME)
4.然后您可以使用以下查询:
4. Then you can query using following:
> documents = db.your_collection.find()
> db.your_collection.insert({'name': 'Nabin Khadka'})
这些python代码可以通过一种方法包装在views.py文件中.喜欢:
These python code can be wrapped in views.py file under a method. Like:
@app.route('/test')
def test():
# All above code
return jsonify(response_dictionary)
然后运行该应用程序,我们可以从浏览器中调用以下网址:
Then running the app, we can call the following url from browser:
https://url_to_server:port/test
这篇关于使用Flask后端从浏览器查询MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文