简体   繁体   English

为包含长度大于 0 的数组的文档查询 firebase

[英]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是否可以在特定字段的数组中的元素数大于 0 的集合中查询 firebase 中的文档

For my example, each document has-a field called 'people', which contains an array of integers (or the array is empty).对于我的示例,每个文档都有一个名为“people”的字段,其中包含一个整数数组(或者该数组为空)。

My search always return 0 documents, but I have documents that I can see when I search in the firestore database admin panel.我的搜索总是返回 0 个文档,但我有在 Firestore 数据库管理面板中搜索时可以看到的文档。

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. 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:假设每个人对象将是 {name:'...',age:'..'} 一个简单的查询是:

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.我试图在我的 Firestore Recycler 中按喜欢该物品的人的数组大小订购物品。 Because there is no such specific query function I created new field "likesNum" .因为没有这样的特定查询函数,所以我创建了新字段"likesNum" To be able to keep it in sync with changes to that array using FieldValue.increment() the field type must be an integer.为了能够使用FieldValue.increment()使其与对该数组的更改保持同步,字段类型必须是整数。

But querying a number value field inside .orderBy() was crashing my app.但是查询.orderBy()数值字段使我的应用程序崩溃。 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.虽然编译器说String.valueOF()不是必需的,但我的问题完美解决。

Maybe that helps someone with same issue.也许这可以帮助有同样问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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