[英]Second orderBy query does nothing in Firestore (react-native-firebase)
EDIT So after reviewing this problem with firebase developer this is working by design and there is no actual way to acheive this. 编辑因此,在与Firebase开发人员一起审查了此问题之后,这是设计使然,并且没有实际的方法来实现。 Offical response from firestore dev:
firestore开发人员的官方回应:
Admittedly certain kinds of queries are difficult, if not feasible, when working with Firestore's NoSQL design.
诚然,在使用Firestore的NoSQL设计时,某些类型的查询很难(如果不可行)。 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.
如果缺乏这种功能被认为是您的用例的关键,那么Firestore可能不是您合适的数据库解决方案。
If you're desperate to achieve something similar then possibly this solution will work for you: https://firebase.google.com/docs/firestore/solutions/arrays#solution_a_map_of_values . 如果您迫切希望实现类似的目标,那么该解决方案可能会为您工作: https : //firebase.google.com/docs/firestore/solutions/arrays#solution_a_map_of_values 。
If not there is always AWS 如果没有,那么总是有AWS
=========================================================================== ================================================== =========================
I'm noticing that the second orderBy
query option does nothing to the data that is returned. 我注意到第二个
orderBy
查询选项对返回的数据不执行任何操作。 Consider the following query: 考虑以下查询:
firebase.firestore()
.collection('posts')
.orderBy('date', 'desc')
.where('date', '>', new Date(1529802276644))
.orderBy('rating', 'desc')
.limit(size)
This query is intending to order posts
by the date
field, then filter then for the last two days, then order them by rating
. 该查询打算按
date
字段对posts
排序,然后过滤,然后筛选过去两天,然后按rating
对posts
排序。
This query is currently returning 3 posts within two days, however is not sorting by rating. 该查询当前在两天内返回3个帖子,但是未按评分排序。 The results I receive are:
我收到的结果是:
[
{date: 3 hours ago, rating: 1},
{date: 5 hours ago, rating: -1},
{date: 9 hours ago, rating: 0},
]
And if I conduct the same query with the last orderBy removed, I actually get the exact same results: 如果我在删除最后一个orderBy的情况下进行相同的查询,则实际上得到的结果完全相同:
firebase.firestore()
.collection('posts')
.orderBy('date', 'desc')
.where('date', '>', new Date(1529802276644))
.limit(size)
produces: 生产:
[
{date: 3 hours ago, rating: 1},
{date: 5 hours ago, rating: -1},
{date: 9 hours ago, rating: 0},
]
I currently have an enabled index over posts. 我目前在帖子上启用了索引。 I'm fairly certain the index is correct because I was previously getting an error about a missing index before this and then fixed it with this index:
我相当确定索引是正确的,因为在此之前我之前遇到过有关丢失索引的错误,然后使用该索引对其进行了修复:
posts | date DESC rating DESC | enabled
I'm pretty sure the problem lies with firebase and not with the library I'm using react-native-firebase. 我很确定问题出在firebase上,而不是我正在使用react-native-firebase的库上。 The firebase firestore SDK I am using is on
5.0.4
I believe 我相信我使用的Firebase Firestore SDK在
5.0.4
EDIT Here is how I actually use the code within my react-native project: 编辑这是我在本机项目中实际使用代码的方式:
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);
The console.log prints out a valid querysnapshot, and the _query field roughly looks like this: console.log打印出有效的querysnapshot,_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"
}
}
}
(bad printing due to copying) (由于复印导致打印质量差)
I'm not certain this is the cause, but in Firebase Realtime Database it is a common mistake, so it's worth a short here too. 我不确定这是原因,但是在Firebase Realtime Database中,这是一个常见错误,因此在这里也值得短谈。
When you convert the QuerySnapshot
documentSnapshots
to a string, it loses any information on the order of the data. 当您将
QuerySnapshot
documentSnapshots
转换为字符串时,它将丢失有关数据顺序的任何信息。
Instead loop over the results with QuerySnapshot.forEach()
to ensure you maintain the order you requested the data in: 而是使用
QuerySnapshot.forEach()
结果,以确保您维护请求数据的顺序:
const documentSnapshots = await baseRequest.get()
documentSnapshots.forEach(document => {
console.log('document', document);
});
If that works, this probably also works: 如果可行,这可能也可行:
const documentSnapshots = await baseRequest.get()
console.log('document', documentSnapshots.docs());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.