Python Eve嵌入式文档查询

问题描述 投票:0回答:2

有没有办法可以在python前夕查询嵌入式文档?例如:我有以下回复: 网址:http://127.0.0.1:5000/shipments/57fafd5bb9211367f5b2006e

{
    "_updated": "Mon, 10 Oct 2016 02:30:51 GMT",
    "track": "57fafd5bb9211367f5b2006d",
    "ref_no": "268771821909",
    "_created": "Mon, 10 Oct 2016 02:30:51 GMT",
    "_id": "57fafd5bb9211367f5b2006e",
    "_etag": "af5af366b7dba18456be6112c59172b1dfe21593"
}

以下是嵌入文档设置为1时的响应:URL:http://127.0.0.1:5000/shipments/57fafd5bb9211367f5b2006e?embedded= {“track”:1}

{
"_updated": "Mon, 10 Oct 2016 02:30:51 GMT",
"track": {
    "_updated": "Mon, 10 Oct 2016 02:37:57 GMT",
    "tracks": [
        {
        "status": "MS",
        "remark": "None",
        "datetime": "Mon, 10 Oct 2016 02:30:51 GMT"
        },
        {
        "status": "DP",
        "remark": "Not Good",
        "datetime": "Mon, 10 Oct 2016 02:31:51 GMT"
        }
        ],
    "_created": "Mon, 10 Oct 2016 02:30:51 GMT",
    "_id": "57fafd5bb9211367f5b2006d",
    "_etag": "9eac811c400d9c8a9507ae83988daeb5a5ec5c6c"
    },
"ref_no": "268771821909",
"_created": "Mon, 10 Oct 2016 02:30:51 GMT",
"_id": "57fafd5bb9211367f5b2006e",
"_etag": "af5af366b7dba18456be6112c59172b1dfe21593"
}

跟踪架构:

schema = {
    'awb_number' : {'type' : 'string'},
    'tracks'     : {'type':'list',
            'schema' : { 'type':'dict',
            'schema'   : {
            'status'   : {'type':'string', 'allowed': STATUS},
            'remark'   : {'type':'string', 'allowed': REMARK},
            'datetime' : {'type':'string'},
            }
        }
    }

}

现在我想查询status数组中最后一个元素的tracks。 Python中的这样的东西:

if tracks[-1]['status'] == 'DP': print 'Do this'.

但我不确定如何在URL中查询相同内容。

python mongodb eve
2个回答
0
投票

我想查询tracks数组中最后一个元素的状态。像Python这样的东西

在完成所有数据库交互和嵌入(如果有)之后,您需要获得所查询内容的响应。您可以通过添加event hook来实现这一目标。

你可以尝试类似的东西

def do_something_with_result(resource, response):
    '''
       resource: contains who called (in your case, shipments probably)
       response: the data which you get back after the fetching
    '''

响应将是包含数据的字典,您可以在其中搜索track。现在需要将此函数作为事件添加到当前烧瓶应用程序中。

from flask import current_app as app

app.on_fetched_item += do_something_with_result

请注意,这将在获取每个项目后触发。如果你想专门针对一个特定的资源on_fetched_item_<resource_name>。然后前一个函数的签名变为do_something_with_result(response)


0
投票

我试过这个,这很有效。

client = MongoClient('localhost', 27017)
db = client['test']
collection = db["interests"]

r=collection.find({})
for data in r:
    for i in data['mylist']:
        if i['status']=='no':
           print("done")

我感兴趣的架构看起来像这样:

interests={
'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10,
'schema':{
    'interest':{
        'type':'objectid',
        'required':True,
        'data_relation': {
            'resource': 'works',
            # make the owner embeddable with ?embedded={"owner":1}
            'embeddable': True
        },
    },
    'mylist':{
        'type':'list',
        'schema':{
            'type':'dict',
            'schema':{
                'myfield':{
                    'type':'string'
                },
                'status':{
                    'type':'string'
                }
            }
        }
    }
  }
}

希望这能解决你的问题..

© www.soinside.com 2019 - 2024. All rights reserved.