【问题标题】:How to put the arrays in order of most recent in Firestore如何在 Firestore 中按最新的顺序排列数组
【发布时间】:2020-05-01 07:58:07
【问题描述】:

所以我想看看是否有办法将特定文档从最新到最旧排序 有没有办法从firestore做到这一点?还是我必须编码?

这是关于我如何获取特定文档 ID 的代码,我只是使用 UUID().string 为我生成了一个

func quoteMeVariables(){

    let quotemodel = quoteME()

    quotemodel.quoteMEid = UUID().uuidString
    quotemodel.author = authorTextfield.text
    quotemodel.QuoteText = quoteMeTextfield.text
    quotemodel.saveQuoteMetoFirebase(quotemodel) //


    hud.textLabel.text = "Your Quote Has Been Sent Successfully!"
    hud.show(in: self.view)
    hud.indicatorView = JGProgressHUDSuccessIndicatorView()
    hud.dismiss(afterDelay: 3.0)

    DispatchQueue.main.asyncAfter(deadline: .now() + 3.0){

        self.dismiss(animated: true, completion: nil)
    }

}

【问题讨论】:

  • 您需要添加时间戳才能根据该时间戳进行排序
  • 有许多解决方案,但您在问题中显示的不是数组。话虽如此,如果您只是在每个文档中添加一个时间戳字段,它们就可以轻松排序。

标签: swift firebase google-cloud-firestore


【解决方案1】:

您可以在 Firestore 中对数组进行排序,但如果您需要对数组进行排序,则在获取它时,您需要在代码中进行。 您需要从服务器向文档添加时间戳。

Go look here, if you don't know how.

然后你可以对你的集合进行 .order(by:timestamp),让它按时间戳排序。

【讨论】:

  • 您无法在 Firestore 中对数组进行排序。数组是服务器无法排序的固定字段序列。序列是按索引的序列。这是 NoSQL 数据库中数组的缺点。
猜你喜欢
  • 2019-07-03
  • 2022-01-07
  • 2017-06-09
  • 2021-01-16
  • 2019-10-10
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多