firebase - 即使在一定范围后在 Firestore 中应用索引后也获得无序数据
问题描述
当我在应用索引后在 Firestore 中添加数据时(通过单击 Android Studio 终端中的链接自动生成索引)。我的数据以我想要的方式添加和查询,直到我的文档 lrNo(我在 Firestore 中的文档中的一个字段)达到 999。之后数据开始从开头添加,这破坏了我的降序。在我的情况下,我的 lrNo 和文档 ID 都是相同的。我得到的结果与我不想要的图像中显示的文档 ID 的顺序相同;我希望它按降序排列。
我已经通过点击下面的链接生成了索引。
Error: FirebaseError: The query requires an index. You can create it here:
https://console.firebase.google.com/v1/r/project/[PROJECT-NAME]/firestore/indexes?create_compo
site=[INDEX EXAMPLE] (failed-precondition)
at Object.throw_ [as throw] (http://localhost:49397/dart_sdk.js:4776:11)
at handleThenable
(http://localhost:49397/packages/firebase/src/storage.dart.lib.js:3237:21)
at handleThenable.throw (<anonymous>)
at onError (http://localhost:49397/dart_sdk.js:35663:38)
at _RootZone.runBinary (http://localhost:49397/dart_sdk.js:35547:58)
at _FutureListener.thenAwait.handleError (http://localhost:49397/dart_sdk.js:30969:50)
at handleError (http://localhost:49397/dart_sdk.js:31482:51)
at Function._propagateToListeners (http://localhost:49397/dart_sdk.js:31505:17)
at _Future.new.[_completeError] (http://localhost:49397/dart_sdk.js:31366:23)
at async._AsyncCallbackEntry.new.callback
(http://localhost:49397/dart_sdk.js:31401:31)
at Object._microtaskLoop (http://localhost:49397/dart_sdk.js:35759:13)
at _startMicrotaskLoop (http://localhost:49397/dart_sdk.js:35765:13)
at http://localhost:49397/dart_sdk.js:31707:9
下面是排序和获取数据的代码:
var temp = await fireStore
.collection("Admin")
.where("phoneNo", isEqualTo: TextFieldData.phoneNo)
.orderBy("lrNo", descending: false)
.getDocuments()
.then((value) => value);
解决方案
如果你需要一个字段来存储一个数值,它应该是一个数字类型的字段,而不是一个字符串。数字和字符串的排序方式不同。字符串总是按字典顺序排序,这意味着它们从左到右使用字符的值,就像字典一样。这使得“1000”小于“999”,因为“1”排序小于“9”。
您的 lrNo 字段是一个字符串,您可以从屏幕截图中看出,因为该值在引号中。它应该是一个数字。确保您的代码正在编写正确的数据类型。
推荐阅读
- javascript - 使用 Vue.js 在 v-for 中设置背景图片
- python - 使用 GoogleNews 抓取新闻时出错 - “没有名为 GoogleNews 的模块”
- python - 通过 vscode jupyter 服务器运行的 Jupyter Notebook 出现 ModuleNotFoundError: No module named from pyspark on Amazon EMR
- javascript - 缩短的 if 语句及其在 JavaScript 中的直接等价物
- f# - F# CSV TypeProvider 在控制台应用程序中不太健壮
- r - 基于列子字符串的行均值
- python - 根据外键过滤项目
- opencl - 非图像 OpenCL 参数上的 read_only 与 const
- deep-learning - builtin_function_or_method' 对象没有属性 'size'
- python - 使用 TLS 的烧瓶应用程序