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);
});
});
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);
});
});
Share
Improve this question
edited Mar 21, 2019 at 20:12
Frank van Puffelen
599k84 gold badges888 silver badges858 bronze badges
asked Mar 21, 2019 at 19:18
amanaman
5391 gold badge8 silver badges19 bronze badges
4 Answers
Reset to default 18This 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.
Indexes are sparse in FS so you can simply do an orderBy and only return documents with populated data on the ordered property:
.collection("users").orderBy("people", "asc")
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.
In case you are trying to filter based on array length in collectionGroup, you can also use !=
operator. Example:
const snapshot = await db.collectionGroup('users').where('people', '!=', []).get();
Note: don't forget to add exemptions in your Firestore index rules for the collectionGroup (ascending, descending, or arrays), otherwise you will get Error: 9 FAILED_PRECONDITION