gpt4 book ai didi

javascript - 谷歌Firestore : Filter documents where sub-key is between given value

转载 作者:搜寻专家 更新时间:2023-11-01 04:34:43 25 4
gpt4 key购买 nike

我正在开发一款浏览器游戏,我在 firestore 中有一个如下所示的集合:

{
title: "doc 1",
requirements: {
level: {
min: 2,
max: 3
}
}
},
{
title: "doc 2",
requirements: {
level: {
min: 6,
max: 8
}
}
},
{
title: "doc 3",
requirements: {
level: {
min: 8,
max: 9
}
}
}

有什么方法可以查询所有符合给定级别的文档,比如当我的级别为 8 时,我只想获取文档“doc 2”和“doc 3”,因为级别要求匹配?

我试过类似的东西

ref.where("requirements.level.min", "<=", level);
ref.where("requirements.level.max", ">=", level);

我还尝试将文档中的结构更改为:

{
title: "doc 1",
requirements: {
level: [2, 3]
}
},
{
title: "doc 2",
requirements: {
level: [6, 7, 8]
}
}

然后像这样过滤

ref.where("requirements.level", "array-contains", level)

但是 firestore 总是返回我所有的文档。

最佳答案

Array contains 在这里似乎是正确的选择。我检查了你的例子 - recreated quite similar structure in my firestore (其他文档具有相同的结构,但级别从 5 到 7)并运行这些查询:

const docs = firebase.firestore().collection('docs');
docs.where('requirements.levels','array-contains',2).get().then((snap) => {
console.log(snap.docs.length); //<- outputs 1, correct
});
docs.where('requirements.levels','array-contains',4).get().then((snap) => {
console.log(snap.docs.length); //<- outputs 0, correct
});
docs.where('requirements.levels','array-contains',5).get().then((snap) => {
console.log(snap.docs.length); //<- outputs 1, correct
});

关于javascript - 谷歌Firestore : Filter documents where sub-key is between given value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53239581/

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