gpt4 book ai didi

javascript - 执行不等式和多个 "array-contains"或类似数组的过滤器

转载 作者:行者123 更新时间:2023-12-03 15:02:57 25 4
gpt4 key购买 nike

我正在尝试使用不等式和多个 array-contains 运行查询.
为简单起见,我用假想数据简化了数据结构。假设在一个名为 grocerie_packages 的集合中我有很多文件。我希望能够根据包裹的名称、包裹的价格以及其中包含的蔬菜和水果来证明搜索。目前的数据结构如下:

{
price: 10,
contained_fruits: ["apple", "pear", "grape"],
contained_vegetables: ["tomato", "cucumber", "carrot"],
package_name: ["f", "fa", "fam", "fami", "famil", "family", "family_", "family_s", "family_si", "family_siz", "family_size"]
}
起初,我使用以下代码根据名称进行查询:
.where("package_name", ">=", searchString)
.where("package_name", "<=", searchString + "\uf8ff");
但后来,当我添加按价格查询的功能时,我遇到了以下问题:

Invalid query. All where filters with an inequality (<, <=, >, or >=) must be on the same field.


我通过创建 package_name 的子字符串数组来解决它.我使用的代码是这样的:
  .where("package_name", "array-contains", package_name_intput)
.where("price", "<", max_price);
我现在遇到的问题是,因为我做了一个 array-contains对于我无法根据所含蔬菜和水果进行查询的查询。我不想在客户端上进行排序,因为在某些情况下,我可能会返回超过所需的数千个项目。我也不能将三个数组合二为一,因为我只想返回一个匹配查询的所有条件的文档。例如,在有人要求 apple 的查询中和 carrot我只想返回包含两者的文件。如果它是一个数组,即使在包中只找到一项,它也会返回文档。我只需要为每个蔬菜和水果提供一项(如 apple )搜索。
我运行这样的查询的最佳方式是什么?我正在尝试找到一个解决方案,在该解决方案中,我不会仅仅为了查询 porpuses 而使用许多标志填充文档。例如:
{
flags: ["apple_tomato_f", "apple_tomato_fa", "apple_tomato_fam", ] //and so on
}

最佳答案

让我先说...
我只想补充一点,我不会在实际项目中这样做。相反,我会在 Cloud Run 上创建一个 API,它执行多个查询、构建数据、临时缓存它并以我想要的任何形式返回结果。
但是,为了让我们的头脑灵活,我将按照以下要求来完成此任务:

  • 必须直接查询 Firestore。
  • 只有一个查询。
  • 没有客户端排序。

  • 大脑时间
    首先重新考虑您的数据库结构。要在单个查询中完成所有这些,您需要存储数据,以便可以使用所有可用的一次性运算符。为此,请将您所有的水果和蔬菜存储为 {[key: string]: boolean}像这样:
    {"grocerie_packages": {
    "abc1": {
    "price": 10,
    "tomato": true,
    "package_name": ["s", "si", "sin", "sing", "singl", "single", "single_s", "single_si", "single_siz", "single_size"]
    },
    "abc2": {
    "price": 20,
    "cucumber": true,
    "tomato": true,
    "package_name": ["f", "fa", "fam", "fami", "famil", "family", "family_", "family_s", "family_si", "family_siz", "family_size"]
    },
    "abc3": {
    "price": 30,
    "package_name": ["s", "si", "sin", "sing", "singl", "single", "single_s", "single_si", "single_siz", "single_size"]
    },
    "abc4": {
    "price": 40,
    "apple": true,
    "tomato": true,
    "package_name": ["f", "fa", "fam", "fami", "famil", "family", "family_", "family_s", "family_si", "family_siz", "family_size"]
    }
    }
    }
    这使得以下所有查询成为可能:
    包裹名称包含“fam”,有西红柿,价格 < 40
        db.collection('grocerie_packages')
    .where('tomato', '==', true)
    .where('package_name', 'array-contains', 'fam')
    .where('price', '<', 40).get()
    .then((snap) => console.log("Results:", snap.docs.map(s => s.id).join(',')));
    Results: abc2 包裹名称中包含“fam”,有西红柿,价格 < 100
        db.collection('grocerie_packages')
    .where('tomato', '==', true)
    .where('package_name', 'array-contains', 'fam')
    .where('price', '<', 100).get()
    .then((snap) => console.log("Results:", snap.docs.map(s => s.id).join(',')));
    Results: abc2,abc4 包裹有 cucumber 和成本> 10
        db.collection('grocerie_packages')
    .where('cucumber', '==', true)
    .where('price', '>', 10).get()
    .then((snap) => console.log("Results:", snap.docs.map(s => s.id).join(',')));
    Results: abc2 包名包含“sing”,有西红柿、 cucumber 和苹果,价格>100
        db.collection('grocerie_packages')
    .where('tomato', '==', true)
    .where('cucumber', '==', true)
    .where('apple', '==', true)
    .where('package_name', 'array-contains', 'sing')
    .where('price', '>', 100).get()
    .then((snap) => console.log("Results:", snap.docs.map(s => s.id).join(',')));
    Results:

    关于javascript - 执行不等式和多个 "array-contains"或类似数组的过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66197664/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com