gpt4 book ai didi

javascript - Second orderBy 查询在 Firestore 中不执行任何操作(react-native-firebase)

转载 作者:行者123 更新时间:2023-11-30 06:24:58 24 4
gpt4 key购买 nike

编辑因此,在与 firebase 开发人员审查了这个问题之后,这是设计使然,没有实际的方法来实现这一点。来自 firestore dev 的官方回应:

Admittedly certain kinds of queries are difficult, if not feasible, when working with Firestore's NoSQL design. If the lack of such capabilities is considered a deal-breaker for your use-case, then it's likely that Firestore may not be the right database solution for you.

如果您迫切希望实现类似的目标,那么此解决方案可能适合您:https://firebase.google.com/docs/firestore/solutions/arrays#solution_a_map_of_values .

如果没有,总有 AWS

============================================= ============================

我注意到第二个 orderBy 查询选项对返回的数据没有任何作用。考虑以下查询:

firebase.firestore()
.collection('posts')
.orderBy('date', 'desc')
.where('date', '>', new Date(1529802276644))
.orderBy('rating', 'desc')
.limit(size)

此查询打算按 date 字段对 posts 进行排序,然后过滤最后两天,然后按 rating 对它们进行排序.

此查询目前在两天内返回 3 个帖子,但未按评级排序。我收到的结果是:

[
{date: 3 hours ago, rating: 1},
{date: 5 hours ago, rating: -1},
{date: 9 hours ago, rating: 0},
]

如果我在删除最后一个 orderBy 的情况下执行相同的查询,我实际上会得到完全相同的结果:

firebase.firestore()
.collection('posts')
.orderBy('date', 'desc')
.where('date', '>', new Date(1529802276644))
.limit(size)

产生:

[
{date: 3 hours ago, rating: 1},
{date: 5 hours ago, rating: -1},
{date: 9 hours ago, rating: 0},
]

我目前有一个启用的帖子索引。我相当确定索引是正确的,因为我之前收到关于缺少索引的错误,然后用这个索引修复它:

posts |  date DESC rating DESC   |    enabled

我很确定问题出在 firebase 而不是我正在使用 react-native-firebase 的库。我使用的 firebase firestore SDK 在 5.0.4 我相信

编辑以下是我在我的 react-native 项目中实际使用代码的方式:

const episodeRef = firebase.firestore().collection('posts');
const baseRequest = episodeRef.orderBy('date', 'desc').where('date', '>', new Date(1529802276644)).orderBy('rating', 'desc').limit(25)
const documentSnapshots = await baseRequest.get()
console.log('documentSnapshots', documentSnapshots);

console.log 打印出有效的查询快照,_query 字段大致如下所示:

_fieldFilters:{
fieldPath:{
string:"date",
type:"string"
},
operator:"GREATER_THAN",
value:{
type:"date",
value:1529802276644
},
}
_fieldOrders:{
0:{
direction:"DESCENDING",
fieldPath: {
string:"date",
type:"string"
},
}
1:{
direction:"DESCENDING"
fieldPath:{
string:"rating",
type:"string"
}
}
}

(由于复印导致打印不良)

最佳答案

我不确定这是不是原因,但在 Firebase 实时数据库中这是一个常见错误,因此这里也值得一提。

当您将 QuerySnapshot documentSnapshots 转换为字符串时,它会丢失有关数据顺序的任何信息。

而是使用 QuerySnapshot.forEach() 遍历结果,以确保您保持请求数据的顺序:

const documentSnapshots = await baseRequest.get()
documentSnapshots.forEach(document => {
console.log('document', document);
});

如果可行,这可能也可行:

const documentSnapshots = await baseRequest.get()
console.log('document', documentSnapshots.docs());

关于javascript - Second orderBy 查询在 Firestore 中不执行任何操作(react-native-firebase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51033748/

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