【问题标题】:Can I use flutter to create an index on firestore?我可以使用颤振在 Firestore 上创建索引吗?
【发布时间】:2020-12-23 00:20:07
【问题描述】:

我正在使用以下颤振代码来查询使用字段时间戳对数据进行排序的 Firestore。

var results = Firestore.instance.collection('post').orderBy('timestamp').getDocuments().then((value) {
  var list = value.documents;
  return list.map((doc) {
    return doc.documentID;
  }).toList();
});    

当我运行此代码时,它会抛出以下异常,说明需要索引:

W/Firestore(31110): (21.3.0) [Firestore]: Listen for Query(app/jQH7Fp9xCZWYiqZRe7lE/post where readAccess array_contains_any [WzKImODx6WYVqdSW3D9Az3xrUnM2, PUBLIC] order by -timestamp, -name ) 失败:Status{code=FAILED_PRECONDITION, description=查询需要索引。你可以在这里创建它:https://console.firebase.google.com/v1/r/project/...

这个例外甚至带有一个很好的链接。打开该链接时,会弹出一个漂亮的 UI,让我能够创建索引,只需单击一下:

问题:虽然上面看起来很简单,但我对此并不满意。我更喜欢能够从fluttercode创建索引。在代码中,我正在寻找类似下面的内容:

Firestore.instance.collection('post').API-TO-CREATE-INDEX('timestamp');

它存在吗?请指教。非常感谢。

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    无法从客户端应用创建索引。您有三个主要选择:

    1. 点击您已经看到的链接。
    2. 使用Firebase CLI 从命令行部署索引。
    3. 使用gcloud CLI 也可以从命令行部署

    另请参阅documentation on managing indexes

    【讨论】:

      猜你喜欢
      • 2020-08-23
      • 2020-08-02
      • 2020-07-20
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 2019-05-26
      • 2021-04-07
      • 1970-01-01
      相关资源
      最近更新 更多