Firestore select where a field is null [duplicate]

荒凉一梦 提交于 2020-01-21 10:07:39

问题


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'
    }
}

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

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

回答1:


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

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

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



来源:https://stackoverflow.com/questions/52431050/firestore-select-where-a-field-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!