繁体   English   中英

仅使用python-eve API向MongoDB请求`_items`

[英]Request `_items` only using python-eve API to a MongoDB

我正在使用python-eve -API(简称eve )访问MongoDB。 插入一些示例数据后,我正在使用Chrome的Postman测试API。

由于eve除了在文档中说明了所请求的数据和HATEOAS指令之外,还提供了其他信息,我只是想知道如何仅请求_items -dictionary(如果使用Python则将其称为dictionary如果使用JSON或JavaScript则将其称为object ) 。

因此,示例请求http://127.0.0.1:5000/packagings/给出了如下所示的响应,我仅对_items -dictionary / -object中包含的数据感兴趣。

可以肯定的是,我可以在接收并存储完整响应后提取所需的数据。 但是,有一种方法可以只请求我感兴趣的数据,以减少额外的数据提取。 一旦收到数据就进行数据处理?

{
    "_links": {
        "self": {
            "href": "packagings",
            "title": "packagings"
        },
        "parent": {
            "href": "/",
            "title": "home"
        }
    },
    "_meta": {
        "max_results": 25,
        "page": 1,
        "total": 1
    },
    "_items": [
        {
            "diameter_dk": 0.0144,
            "_created": "Tue, 17 Nov 2015 21:15:37 GMT",
            "factor_fa": 2.1,
            "_id": "564b98f955c40f29843128df",
            "free_volume": 0.89,
            "title": "raschigring10x10x0.5",
            "_updated": "Tue, 17 Nov 2015 21:15:37 GMT",
            "_links": {
                "self": {
                    "href": "packagings/564b98f955c40f29843128df",
                    "title": "Packaging"
                }
            },
            "specific_weight": 920,
            "title_hr": "Raschig-Ring 10x10x0.5",
            "specific_surface": 500,
            "specific_number": 770000,
            "_etag": "bcb4080b61028405babcd960196d27208c3eabd3"
        }
    ]
}

您可以通过在配置设置中设置HATEOAS = False来禁用HATEOAS。 这将大大减少有效负载,使其更适合您的用例。

编辑:您还可以选择通过将回调函数挂接到on_fetched_resource事件来转换响应有效负载。

from eve import Eve

def on_fetched_resource(resource, response):
    del(response['_links'])
    del(response['_meta'])

    # would result in an empty JSON document
    # del(response['_items'])

app = Eve()
app.on_fetched_resource += on_fetched_resource

if __name__ == '__main__':
    app.run()

由于响应是字典(毕竟是JSON),因此您仍然需要为documents数组提供键。

暂无
暂无

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

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