使用flask服务器从浏览器查询MongoDB [英] Querying MongoDB from browser using flask server
本文介绍了使用flask服务器从浏览器查询MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谢谢。
lockquote
$ pre>
from pymongo import MongoClient
client = MongoClient(MONGO_URL)
db = getattr(client,DATABASE_NAME)
$ p $ 4>。 4。然后你可以使用下面的查询:
> documents = db.your_collection.find()
> db.your_collection.insert({'name':'Nabin Khadka'})
这些python代码可以被包装在一个方法下的views.py文件中。像:
@ app.route('/ test')
def test():
#以上代码
返回jsonify(response_dictionary)
然后运行应用程序,我们可以调用从浏览器下面的URL:
https:// url_to_server:port / test
I am building an interactive visualization tool that lets users query data which is then visualized using D3, Flask as the server and MongoDB as the database. My question is how do i query the MongoDB with the user input and render this to the server.
Thanks.
解决方案
- Install the mongodb and run its server (in terminal type mongo)
- Install pymongo (python package)
- Then create an instance of the pymong using:
from pymongo import MongoClient client = MongoClient(MONGO_URL) db = getattr(client, DATABASE_NAME)
4. Then you can query using following:
> documents = db.your_collection.find()
> db.your_collection.insert({'name': 'Nabin Khadka'})
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屋!
查看全文