Python 3.x Firestore-使用游标和范围运算符进行查询
我在使用Python3.7,Firestore有一个问题,我正在尝试执行一个范围查询,其中该字段位于文档中名为“time”的映射中。问题是,官方文档中说,不同的字段不能在带有范围运算符的查询中使用 地图字段:Python 3.x Firestore-使用游标和范围运算符进行查询,python-3.x,google-cloud-firestore,Python 3.x,Google Cloud Firestore,我在使用Python3.7,Firestore有一个问题,我正在尝试执行一个范围查询,其中该字段位于文档中名为“time”的映射中。问题是,官方文档中说,不同的字段不能在带有范围运算符的查询中使用 地图字段: time: {from: 1030 (number), to: 1600 (number)} (map) 所以我不能做: def get_docs(time): docs_ref = client.collection("COLLECTION_NAME")
time: {from: 1030 (number), to: 1600 (number)} (map)
所以我不能做:
def get_docs(time):
docs_ref = client.collection("COLLECTION_NAME") \
.where("time.from", "<=", time) \
.where("time.to", ">=", time)
我不知道范围运算符的限制是否会影响光标,如果是这种情况,请告诉我。我试图运行您的示例,但发现了此错误
400不等式筛选器属性和第一个排序顺序必须相同:time.from和time.to
这是因为只有,其中运算符才允许出现在order\u by
操作所使用的字段上
处理查询的主要逻辑
client.collection("COLLECTION_NAME") \
.where("time.from", "<=", time) \
.where("time.to", ">=", time)
这是我的结果(from
=1100)
您是对的,错误是由于在order\u by
字段中没有where
。我正在使用模拟器,由于某种原因,该错误没有显示出来。否则,在您的响应中,get_docs
函数按字段排序到
,该字段与从
中获取光标的字段不同,这将以不同的顺序返回文档列表,光标将位于X位置,可能会遗漏有效的文档。@federicoforzano元素的顺序将不同,因为我们有不同的过滤器(相同的数据),但这种方法有效,因为只需要知道何时停止第二个查询(光标)请用一些数据尝试这种方法,并告诉我这是否有效
documents:
doc1 => time: {from: 1030, to: 1600}
doc2 => time: {from: 1000, to: 1300}
doc3 => time: {from: 1200, to: 1900}
doc4 => time: {from: 1100, to: 1700}
documents order_by time.to ascending:
doc2 => time: {from: 900, to: 1000}
doc1 => time: {from: 1030, to: 1600}
doc4 => time: {from: 1100, to: 1700}
doc3 => time: {from: 1200, to: 1900}
time = 1100
get_cursor(time) => doc1
get_docs(time):
expected result: {doc1, doc4}
obtained result: {}
client.collection("COLLECTION_NAME") \
.where("time.from", "<=", time) \
.where("time.to", ">=", time)
{'time': {'from': 1000, 'to': 1300}}
{'time': {'from': 1030, 'to': 1600}}
{'time': {'from': 1100, 'to': 1700}}