Firestore选择字段为空的位置 [英] Firestore select where a field is null

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

问题描述

如何选择没有特定字段的所有文档?我有以下结构:

How can I select all docs that do not have a certain field? I have the following structure:

user = {
    firstName: 'blabla',
    lastName: 'bloblo',
    address: {
        city: 'xxxx',
        state: 'xxx'
    }
}

我曾尝试与null进行比较,但没有成功.

I have tried comparing with null, but wasn't successful.

let db = firebase.firestore();  
let querySnapshot = await db
    .collection("users")
    .where("address", "==", null)
    .get();

推荐答案

不可能选择所有没有特定字段的文档".

Selecting "all docs that do not have a certain field" is not possible.

您将通过观看名为开始了解Cloud Firestore"的非常出色的Firestore视频系列(

As you will learn by watching the very good Firestore video series called "Get to know Cloud Firestore" (https://www.youtube.com/playlist?list=PLl-K7zZEsYLluG5MCVEzXAQ7ACZBCuZgZ), in particular the 2nd video, queries in Firestore are based on indexes, and it is not possible to index on a field that is NOT in the document, or a field that does not have a certain value (i.e. a "not equal" operator).

但是,有一种解决方法是查询包含具有空值数据类型的属性的文档,请参见

However there is a workaround which consists in querying documents that contain properties with a null value data type, see How to query Cloud Firestore for non-existing keys of documents

还要有趣的一点是,即使不是您要的内容,该技术也可以用于查询不为null的值

Also interesting to note (even if it is not what you are asking for) is this technique for querying for values that are not null Firestore select where is not null

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

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