简体   繁体   中英

query firebase for documents that contain an array of length > 0

Is it possible to query firebase for documents in a collection where the number of elements in an array of a particular field is greater than 0

For my example, each document has-a field called 'people', which contains an array of integers (or the array is empty).

My search always return 0 documents, but I have documents that I can see when I search in the firestore database admin panel.

import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
admin.initializeApp();

var db = admin.firestore();
export const helloWorld = functions.https.onRequest(function(req,res)
{
    var all_users = db.collection('users');
    var query = all_users.where("people.length", ">", 0).get().then(snapshot => 
    {
        let docs = snapshot.docs;

        //return 1st document found

        res.send(docs[0].data());
    });

    query.catch(error =>
    {
        res.status(500).send(error);
    });

});

This is not possible with Firestore. The only things you can query for array type fields is the exact contents of some element in the array.

Your alternative for filtering on the size of any array is to use an integer field to record the number of elements in the array, and keep it in sync with changes to that array.

索引在 FS 中是稀疏的,因此您可以简单地执行 orderBy 并仅返回具有已排序属性上填充数据的文档:

.collection("users").orderBy("people", "asc")

EDIT

Does not work anymore.


Old Answer

It is possible with some sort of imagination and only if you know what would be inside of the array (NOT HOW MUCH, just the properties of the objects, or the type of primitive)

Lets say each people object will be {name:'...',age:'..'} A simple query would be:

firestore.collection('people').where('people.0.name','>','')

In case of primitives

firestore.collection('people').where('people.0','>','')

This way you will search for an array that has the first item name property not empty. If the array does not have a first property, then it's empty.

IMPORTANT: like i said, only works if you know what kind of object will be there

I was trying to order items in my Firestore Recycler by size of array of people who likes that item. Because there is no such specific query function I created new field "likesNum" . To be able to keep it in sync with changes to that array using FieldValue.increment() the field type must be an integer.

But querying a number value field inside .orderBy() was crashing my app. So i tried :

Query query = placesRef.orderBy( String.valueOf("likesNum"), Query.Direction.DESCENDING).limit(5);

And it worked. Although the compiler is saying String.valueOF() is not necessary, my problem is perfectly solved.

Maybe that helps someone with same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM