简体   繁体   English

二阶通过查询在Firestore中不执行任何操作(react-native-firebase)

[英]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排序,然后过滤,然后筛选过去两天,然后按ratingposts排序。

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.

相关问题 一次从 Auth 和 Firestore 中删除具有 react-native-firebase 的用户的正确方法? - The correct way to delete a user with react-native-firebase from Auth and Firestore at once? 无法从 React-Native-Firebase(v6) Firestore 获取数据:undefined 不是函数(靠近“...this._firestore.native.collectionGet...”) - Can't get data from React-Native-Firebase(v6) Firestore: undefined is not a function (near '...this._firestore.native.collectionGet...') Firebase Firestore与orderby多个查询 - firebase firestore multiple query with orderby react-native firestore:使用不同字段使用 where() 和 orderBy() 进行查询 - react-native firestore: Query with where() and orderBy() using different fields react-native-firebase重新发送消息代码 - react-native-firebase resend message code react-native-firebase getInitialNotification循环 - react-native-firebase getInitialNotification loop React-Native-Firebase:RTDB 模拟器 - React-Native-Firebase : RTDB Emulator 在 useEffect 中使用“array-contains”的 react-native-firebase 查询保持刷新组件 - react-native-firebase query with 'array-contains' in useEffect keeps refreshing component OrderBy + foreach + dataRef 在 Firestore/Firebase 中不起作用 - OrderBy + foreach + dataRef does not work in Firestore/Firebase 使用 react-native-firebase 时添加对 Android 通知的回复 - Add replies to Android notifications while using react-native-firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM