gpt4 book ai didi

python - 为什么我的 Firebase Firestore 查询不在 order_by 中?

转载 作者:行者123 更新时间:2023-12-04 10:49:17 24 4
gpt4 key购买 nike

我已经阅读了其他几个问题,但它们似乎都是将日期存储为字符串而不是时间戳的地方。这是我的领域:

enter image description here

我试图按照我的 upload_timestamp 字段的顺序返回文档,但它们似乎是随机顺序......这是我的 Python Flask 代码:

@app.route('/uploads/<string:last_doc>', methods=['GET'])

def uploads(last_doc=False):

rentalAgreements = db.collection('rentals')
query = rentalAgreements.where('username', '==', 'johnny5isalive').order_by('upload_timestamp', direction=firestore.Query.ASCENDING).limit(5)
results = query.get()

response = {}
for doc in results:
response[doc.id] = doc.to_dict()
last_doc = doc.id
print(doc.id)
response['last_doc'] = last_doc

return jsonify({"response" : response})

结果是这样的:
{
"response": {
"4cbacd525c9842bbae29f2f9ef15109f": {
"filename": "RL 20 DECEMBER 2019v2.csv",
"rental_list": "4cbacd525c9842bbae29f2f9ef15109f",
"status": "imported",
"upload_timestamp": "Wed, 01 Jan 2020 19:53:42 GMT",
"username": "johnny5isalive"
},
"82e8f254607a49699d7986bf7cd7cbae": {
"filename": "RL 20 DECEMBER 2019v2.csv",
"rental_list": "82e8f254607a49699d7986bf7cd7cbae",
"status": "imported",
"upload_timestamp": "Sat, 28 Dec 2019 08:44:08 GMT",
"username": "johnny5isalive"
},
"874d39a8cfb246bbbc5a722498dff651": {
"filename": "RL 20 DECEMBER 2019.csv",
"rental_list": "874d39a8cfb246bbbc5a722498dff651",
"status": "imported",
"upload_timestamp": "Fri, 27 Dec 2019 19:52:52 GMT",
"username": "johnny5isalive"
},
"adf6b21511774656a5ed0391618e5ad9": {
"filename": "RL 20 DECEMBER 2019.csv",
"rental_list": "adf6b21511774656a5ed0391618e5ad9",
"status": "imported",
"upload_timestamp": "Wed, 01 Jan 2020 19:53:36 GMT",
"username": "johnny5isalive"
},
"c738e86053704fd6b3ce70395696b950": {
"filename": "RL 20 DECEMBER 2019vparent-child.csv",
"rental_list": "c738e86053704fd6b3ce70395696b950",
"status": "imported",
"upload_timestamp": "Wed, 01 Jan 2020 16:57:35 GMT",
"username": "johnny5isalive"
},
"last_doc": "4cbacd525c9842bbae29f2f9ef15109f"
}
}

最佳答案

这是因为我们使用的 dict ( response = {} ) 并不意味着要订购。因此,您按顺序从 Firestore 获取值,但是在插入到 dict 后,该顺序丢失了。

您可以使用 OrderedDict。我找到了很好的引用和示例 here

我希望它会有所帮助:)

关于python - 为什么我的 Firebase Firestore 查询不在 order_by 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59560410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com