通过比较两个文档的字段值来定义Firestore查询 [英] Defining a Firestore query by comparing two document's fields values

查看:31
本文介绍了通过比较两个文档的字段值来定义Firestore查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Cloud Firestore上迈出第一步,而我被困在这一步上.我正在寻找一个where查询,以返回限制为> = 的值.

I'm having my first steps with Cloud Firestore, and I am stuck on this one. I'm looking for a where query to return me values that are >= the limit.

  [{
    'limit': 100,
    'value': 200 
  }, 
  {
    'limit': 50,
    'value: 50,
  },
  {
    'limit': 95,
    'value': 90, 
  }]

我正在Cloud Function中使用Firebase Admin Python SDK.

I am using the Firebase Admin Python SDK in a Cloud Function.

有任何提示吗?

推荐答案

如Alex所述,在使用Firestore执行查询时,无法比较两个文档的字段值.

As Alex has explained, there is no way to compare two document's fields values when executing a query with Firestore.

一种可能的方法是将文档保存到数据库时保存不等式的结果.如果在保存检查"文档时知道两个值( limit value ),则可以添加第三个字段,例如, valueAboveLimit的值为 True False ,该值是您在保存检查"文档时(即从前端使用相应的客户端SDK或从具有Firebase Admin Python SDK的服务器上获取,具体取决于您编写Firestore文档的方式).

One possible approach, would be to save the result of the inequality when you save the document to the database. If, when you save a "check" document you know the two values (limit and value), you could add a third field named, for instance,valueAboveLimit with a value of True or False, that you calculate when you save the "check" document (i.e. from your front-end with the corresponding client SDK or from a server with the Firebase Admin Python SDK, depending on how you write the Firestore documents).

然后您的查询将很容易:

Then your query will be easy:

database = firestore.client()
col_checks = database.collection('checks')
query_checks = col_checks.where('valueAboveLimit', '==', True)
results = query_checks.get()


如果在保存Firestore文档时没有 limit value 的值(例如 limit 字段是在稍后编写的),您可以使用触发


In case you don't have the values of limit and value at the time you save the Firestore document (for example the limit field is written later), you could use a Cloud Function that is triggered when the Firestore document is changed and, if the two fields are present, calculates and saves this valueAboveLimit field.

这篇关于通过比较两个文档的字段值来定义Firestore查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆