查询比较为空字段 [英] Query comparisons with null field

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

问题描述

我的Firestore集合的文档带有一个字符串字段,该字段可以为 null .

My Firestore collection has documents with a string field, which can be null.

我期望如果我查询:

Collection("products").Where("producedDate", "<", "2018-01-15")

我将获取所有"producedDate"早于"2018-10-15"的产品,包括那些"producedDate"为空的产品.

I would get all products whose "producedDate" is earlier than "2018-10-15", including those whose "producedDate" is null.

但是实际上我没有得到 null .

But actually I am not getting the nulls.

这是故意的还是一个错误?

Is this intended or it's a bug?

推荐答案

它旨在以这种方式工作.文档指出:

It was intended to work that way. The documentation states that:

当查询涉及具有混合类型值的字段时,云Firestore使用基于内部的确定性排序表示形式.以下列表显示了顺序:

When a query involves a field with values of mixed types, Cloud Firestore uses a deterministic ordering based on the internal representations. The following list shows the order:

  1. 空值
  2. 布尔值
  3. 整数和浮点值,按数字顺序排序
  4. 日期值
  5. 文本字符串值
  6. [...]

请注意,仅当您使用混合类型的值运行查询时,它才遵循此顺序.在您的查询中,您传递的是 Date 值,这意味着它将仅查询 Date 类型的值而不是其他(例如 null ).

Note that it only follows this order when you're running a query with values of mixed types. In your query you're passing a Date value, which means it will only query on values of Date type and not the others (like null for example).

为了获取 null 值,您可以通过添加第二个 Where :

In order to get the null values, you can create a compound query, by adding a second Where:

Collection("products").Where("producedDate", "<", "2018-01-15").Where("producedDate", "==", null)

这篇关于查询比较为空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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