简体   繁体   English

PyMongo 和 Flask-PyMongo 库之间的区别

[英]Difference between PyMongo and Flask-PyMongo libraries

I'm new to PyMongo and Flask, I have completed the tutorial for flask though and feel rather comfortable with it.我是 PyMongo 和 Flask 的新手,不过我已经完成了 Flask 的教程,并且对它感到很舒服。 I am now trying to implement a flask server with MongoDb and I'm not sure how to progress.我现在正在尝试使用 MongoDb 实现一个 Flask 服务器,但我不确定如何进行。

I see there are two libraries, PyMongo and Flask-PyMongo.我看到有两个库,PyMongo 和 Flask-PyMongo。 It's isn't clear to me which I should use, or if I need to use both.我不清楚应该使用哪个,或者我是否需要同时使用两者。 How they interoperate and such...它们如何互操作等等......

First, i'm trying to connect to a mongodb, I have that running in the background and I can see connections whenever I start up my flask server, so that must be working.首先,我正在尝试连接到一个 mongodb,我在后台运行它,每当我启动我的 Flask 服务器时我都可以看到连接,所以它必须工作。 PyMongo offers methods to connect to specific database using db = client['test-database'] . PyMongo 提供了使用db = client['test-database']连接到特定数据库的方法。 Flask-pymongo seems to just give a db connection from nowhere when using mongo = PyMongo(app) then mongo.db for access to the db.当使用mongo = PyMongo(app)然后使用mongo.db访问数据库时,Flask-pymongo 似乎只是从无处提供数据库连接。

Being new to mongo as well this is all confusing for me, I was hoping someone would be able to give me a clear answer to all my questions, searches around the web don't reveal many results for flask-pymongo library.作为 mongo 的新手,这对我来说很困惑,我希望有人能够给我一个明确的答案来回答我的所有问题,网上搜索并没有显示出很多flask-pymongo 库的结果。

I did have a look at this question: What is the relationship between flask, mongokit, pymongo, flask-pymongo?我确实看过这个问题: flask、mongokit、pymongo、flask-pymongo 之间的关系是什么? , but it did not clear anything up for me. ,但它并没有为我清除任何东西。

The main difference is that flask-pymongo is a wrapper of pymongo ready to work on flask's application environment.主要区别在于flask-pymongo 是pymongo 的包装器,可以在flask 的应用程序环境中工作。

You can configure the database connection within flask application config object.您可以在 Flask 应用程序配置对象中配置数据库连接。

Flask-pymongo also implements helper methods on top of pymongo.. Flask-pymongo 还在 pymongo 之上实现了辅助方法。

For example: pymongo have find() method, while flask-pymongo have an extension named find_or_404() which raise a not found exception if the item does not exist, and so on..例如:pymongo 有find()方法,而flask-pymongo 有一个名为find_or_404()的扩展名,如果该项目不存在,则会引发未找到异常,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM