Python 云Firestore:文本搜索

Python 云Firestore:文本搜索,python,python-3.x,google-cloud-firestore,Python,Python 3.x,Google Cloud Firestore,我试图通过欧洲/Chisinau时区查找用户。此类用户的一个示例: 'device':{ 'from':'{"status":"success","country":"Moldova","countryCode":"MD","zip":"MD-3300","timezone":"Europe/Chis

我试图通过
欧洲/Chisinau
时区查找用户。此类用户的一个示例:

'device':{
    'from':'{"status":"success","country":"Moldova","countryCode":"MD","zip":"MD-3300","timezone":"Europe/Chisinau"}'
}
这里的
from
字段是一个字符串。以下是我尝试使用的:

In [1]: from google.cloud import firestore
   ...: firestore_client = firestore.Client.from_service_account_json('creds.json')
   ...: ref = firestore_client.collection('databases').document('testing')
In [2]: ref.collection(u'users').where(u'device.from', u'>=', u'Europe/Chisinau').where(u'device.from', u'<=', u'Europe/Chisinau').get()
Out[2]: []
[1]中的
:从google.cloud导入firestore
…:firestore\u client=firestore.client.from\u service\u account\u json('creds.json'))
…:ref=firestore\u client.collection('databases')。document('testing'))

在[2]中:参考集合(u'users')。其中(u'device.from',u'>=',u'Europe/Chisinau')。其中(u'device.from',u'看起来您的
from
字段是一个字符串值,您在其中编码了一个JSON对象。在这种情况下,您将无法查询JSON对象的各个部分,因为Firestore只能从字符串开始搜索。因此,这可能会起作用:

ref.collection(u'users').where(u'device.from', u'>=', u'{"status":"success"').where(u'device.from', u'<=', u'{"status":"updated"')
现在,您可以使用以下命令执行查询:

ref.collection(u'users').where(u'device.from.timezone', u'==', u'Europe/Chisinau')

你好,弗兰克。谢谢你的回复。这很有效,但我也有欧洲/基辅时区的用户。我看不到你在发出请求时指定时区。也就是说,我如何在不同的时区搜索用户,例如美国/阿拉斯加?我希望从我的回答中可以清楚,为什么你当前的查询永远无法工作:数据根本不在同一个时区中可以查询的格式,因为您尝试按字符串结尾进行筛选,Firestore不支持该格式。将JSON字符串转换为映射后,数据的格式正确,我只是重写了您的条件,以使用一个
=
而不是两个范围条件。如果您在这之后仍然有问题,我建议POST更新数据结构的新问题,以及您正在触发的查询。
ref.collection(u'users').where(u'device.from.timezone', u'==', u'Europe/Chisinau')