Firestore 数组中的排序顺序 [英] Sort order in Firestore arrays

查看:27
本文介绍了Firestore 数组中的排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更多地了解 Firebase 中的数组.目前,我将地图存储在数组中,其中地图内的一个字段是 position,我可以在我的移动应用程序中使用它对数组进行排序,并按以下顺序显示结果位置.

I'm trying to understand arrays in Firebase a bit more. Currently, I'm storing maps in arrays, where one of the fields inside the map is a position that I can use in my mobile app to sort the array with on retrieval and show results in the order of position.

关于 Firebase 的文档说:

数组按元素排序.如果元素相等,则数组按长度排序.

Arrays are sorted by elements. If elements are equal, the arrays are sorted by length.

例如,[1, 2, 3] <[1, 2, 3, 1] <[2].

然后还有一个部分描述了地图的排序方式:

And then there's a section describing how maps are sorted as well:

键排序总是排序的.例如,如果你写 {c: "foo", a: "bar", b: "qux"} 地图按键排序并保存为 {a: "foo", b: "bar", c: "qux"}.

Key ordering is always sorted. For example, if you write {c: "foo", a: "bar", b: "qux"} the map is sorted by key and saved as {a: "foo", b: "bar", c: "qux"}.

Map 字段按键排序并按键值对进行比较,先比较键,然后比较值.如果第一个键值对相等,则比较下一个键值对,依此类推.如果两个映射以相同的键值对开始,则考虑映射长度.例如,以下地图按升序排列:

Map fields are sorted by key and compared by key-value pairs, first comparing the keys and then the values. If the first key-value pairs are equal, the next key-value pairs are compared, and so on. If two maps start with the same key-value pairs, then map length is considered. For example, the following maps are in ascending order:

{a: "aaa", b: "baz"}
{a: "foo", b: "bar"}
{a: "foo", b: "bar", c: "qux"}
{a: "foo", b: "baz"}
{b: "aaa", c: "baz"}
{c: "aaa"}

但后来我在 Firestore 中尝试了这个:我在上面的例子中混淆了地图的顺序,并将它们存储在一个数组中:

But then I tried this in Firestore: I jumbled up the order of the maps in the above example, and stored them in an array:

data= [{"c": "aaa"}, {"a": "aaa", "b": "baz"}, {"a": "foo", "b": "baz"}, {"b": "aaa", "c": "baz"}, {"a": "foo", "b": "bar", "c": "qux"}, {"a": "foo", "b": "bar"}]

在插入 Firestore 文档时,数组没有排序!虽然键本身确实在单个 Map 中排序,但数组中的元素保持相同的顺序.

And upon inserting into a Firestore document, the array did not get sorted! While the keys themselves do get sorted within a single Map, the elements in the array stay in the same order.

那么当元素是 Map 时,数组中的排序是否有效?下面是我在 Firestore 中存储的示例:

So does sorting in arrays even work when elements are Maps? Here's an example of what I'm storing in Firestore:

{
    "car_collection": {
        "models": {
            data: [
                {
                    "model": "Honda",
                    "color": "black",
                    "position": 0
                },
                {
                    "model": "Hyundai",
                    "color": "red",
                    "position": 1
                },
                {
                    "model": "Chevrolet",
                    "color": "yellow"
                    "position": 2
                }
            ]
        }
    }
}

我正在存储一个名为位置"的附加字段,并且每次检索时地图的顺序都保持不变.想知道我是否需要存储这个字段,否则数据将按照我存储的顺序进行排序.

I'm storing an additional field called "position", and the order of maps stays the same on every retrieval. Wondering if I even need to store this field, or data will be sorted in the order that I store it in.

推荐答案

已向 Google 提交了一张票以改进 Array 类型的文档,我认为通过一些冒烟测试来看它很有帮助且准确.

Submitted a ticket to Google to improve the documentation for Array type, and I think it's helpful and accurate as seen through some smoke testing.

https://firebase.google.com/docs/firestore/manage-data/data-types

将当前版本复制粘贴到此处:

Copy-pasting the current version here:

一个数组不能包含另一个数组值作为其元素之一.

An array cannot contain another array value as one of its elements.

在数组中,元素保持分配给它们的位置.对两个或多个数组进行排序时,数组将根据其元素值进行排序.

Within an array, elements maintain the position assigned to them. When sorting two or more arrays, arrays are ordered based on their element values.

比较两个数组时,比较每个数组的第一个元素.如果第一个元素相等,则比较第二个元素,依此类推,直到找到差异为止.如果数组用完要比较的元素但到该点为止相等,则较短的数组排在较长的数组之前.

When comparing two arrays, the first elements of each array are compared. If the first elements are equal, then the second elements are compared and so on until a difference is found. If an array runs out of elements to compare but is equal up to that point, then the shorter array is ordered before the longer array.

例如,[1, 2, 3] <[1, 2, 3, 1] <[2].数组 [2] 具有最大的第一个元素值.数组 [1, 2, 3] 的元素等于 [1, 2, 3, 1] 的前三个元素,但长度较短.

For example, [1, 2, 3] < [1, 2, 3, 1] < [2]. The array [2] has the greatest first element value. The array [1, 2, 3] has elements equal to the first three elements of [1, 2, 3, 1] but is shorter in length.

因此,您似乎可以放心地期望在 Firestore 中维护元素的顺序,同时了解添加/删除的影响.

So it seems you can safely expect the order of elements to be maintained in Firestore, while understanding the effects of addition/removal as well.

这篇关于Firestore 数组中的排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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