Second orderBy query在Firestore(react-native-firebase)中不做任何事情



EDIT因此,在与Firebase开发人员审查此问题后,这是设计工作,并且没有实际的方法可以实现这一目标。消防店开发者的官方回应:

诚然,某些类型的查询即使不可行,也很困难, 当使用Firestore的NoSQL设计时。如果缺乏这样的 功能被认为是您的用例的交易破坏者,那么它是 Firestore 可能不是适合您的数据库解决方案。

如果您迫切希望实现类似目标,那么此解决方案可能适合您: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

我很确定问题出在火基地上,而不是我正在使用的 react-native-firebase 库。我相信我正在使用的火力基地消防商店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);

控制台.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实时数据库中这是一个常见的错误,所以这里也值得一试。

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

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

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

如果这有效,这可能也有效:

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

最新更新