【问题标题】:Firebase sort by alphabetical orderFirebase 按字母顺序排序
【发布时间】:2022-01-06 21:22:54
【问题描述】:

可以按字母顺序对数据库列表进行排序吗? 我尝试了 sortby() 函数,但它不能正常工作

var snapshot = await firebase.firestore().collection('Locations').where(
            "category", "==", type).orderBy('name','desc').get();

例如在数据库中: 1-名称:鸡 2-名称:猪 3- 名字:鸭子

按名称排序后,顺序应该是 鸡鸭猪

这是数据库的外观,以及我运行代码后的结果。

这是我的完整代码:

export default function filtercat(type,sortformat){

    var locList = []

    const filterbytype = async() =>{

        if (sortformat == 'rating'){

             // will filter by the type of location, and then sort by rating
        var snapshot = await firebase.firestore().collection('Locations').where(
            "category", "==", type).orderBy('rating','desc').get();
        
        // this is for All category, it will display all locations
        if (type == 'All'){
            var snapshot = await firebase.firestore().collection('Locations').orderBy('rating','desc').get()
        }
        if (snapshot.empty){
            console.log('No matching documents');
            return;
        }
        
        snapshot.forEach(doc=> {
            locList.push(doc.data());
        })
        
        }

        if (sortformat == 'name'){

            // will filter by the type of location, and then sort by alphab
        var snapshot = await firebase.firestore().collection('Locations').where(
            "category", "==", type).orderBy('name','asce').get();
        // this is for All category, it will display all locations
        if (type == 'All'){
            var snapshot = await firebase.firestore().collection('Locations').orderBy('name','desc').get()
        }
        if (snapshot.empty){
            console.log('No matching documents');
            return;
        }
        
        snapshot.forEach(doc=> {
            locList.push(doc.data());
        })
        }
       
        
    }
    
    return filterbytype().then(()=>{
        console.log(locList)
        return locList;
    })
    // filterbytype()
}

有问题的部分是排序格式是名称。

这是firebase中的索引

【问题讨论】:

  • 很难说你的代码为什么不起作用。您可以编辑您的问题以显示:1)您从 Firebase 控制台查询的文档的屏幕截图吗? 2) 处理snapshot 并打印(错误)结果的代码?
  • @FrankvanPuffelen 嗨弗兰克,我刚刚更新了我的问题,如您所见,结果没有正确排序名称。
  • 感谢您的更新。我仍然缺少#2 的代码,不幸的是这可能是相关的。我还立即注意到您的日志输出将rating 显示为一个数字,但在数据库的屏幕截图中它存储为一个字符串。将数字存储为字符串是导致排序问题的常见原因,因为字符串是按 lexicographically 排序的。
  • @FrankvanPuffelen 嗨弗兰克,我刚刚更新了我的完整代码。按评级排序工作正常,但按字母顺序排序时不起作用。看起来是从 Z 到 A 而不是 A 到 Z
  • 这将与此订单子句匹配:orderBy('rating','desc'),不是吗?

标签: node.js firebase react-native google-cloud-firestore


【解决方案1】:

firebase whereorderBy 的限制很少

如果您包含具有范围比较(、>=)的过滤器,您的 第一次排序必须在同一个字段上:

因此,在您的情况下,您在category 字段上具有where 条件,在name 上具有orderBy 条件,因此根据firebase 文档,它将不起作用。

Here是参考文档

【讨论】:

  • 嗨拉维,但是当我按评级排序时,它工作正常。 where("category", "==", type).orderBy('rating','desc').get(); 这样它会按等级从高到低排序。你能告诉我如何按字符 alphab 排序吗
  • 很奇怪,因为它也不应该与评级一起使用,您能否验证评级是否实际上正在排序,或者数据存储中的数据本身是否为评级的排序格式。因为当我遇到同样的问题时,我已经优先考虑我在 firestore 查询中完成 where 子句并在本地数组中完成排序。
  • 是的,它确实有效,但我需要先在 firebase 中创建索引。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 2011-01-30
  • 2019-04-05
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多