简体   繁体   English

如何按属性值从Firebase / Firestore获取文档

[英]How do I get documents from Firebase / Firestore ordered by property value

I have 6 documents in a "categories" collection that look like: 我在“类别”集合中有6个文档,如下所示:

{
  color: "#eee",
  index: 6,
  name: "Restaurants",
}

I want to retrieve these documents ordered by the index property, ordered from least to most. 我想检索按索引属性排序的这些文档,从最小到最大排序。 I am able to get all the documents, but I'm not sure how to order the results by index. 我可以获取所有文档,但是不确定如何按索引对结果进行排序。 I am also not sure whether I should query them from Firestore and then only order them once they are on the client. 我也不确定是否应该从Firestore查询它们,然后仅在客户端上对其进行订购。

Today my query looks like: 今天,我的查询看起来像:

var db = firebase.firestore();    
db.collection("categories").get().then((querySnapshot) => {
      let categories = []
      querySnapshot.forEach((doc) => {
        categories.push(doc.data())
      });
      this.setState({categories: categories});
    });

Thank you! 谢谢!

Hi astrojams1 great question. 嗨astrojams1个好问题。 Modify your query as so: 这样修改查询:

db.collection("categories").orderBy("index")
  .get()
  .then((querySnapshot) => {
    let categories = []
    querySnapshot.forEach((doc) => {
      categories.push(doc.data())
    });
    this.setState({categories: categories});
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取 Firebase Cloud Firestore 中存在/不存在特定字段的文档? - How do I get documents where a specific field exists/does not exists in Firebase Cloud Firestore? 如何在本机反应中从 firebase 火库中获取除一份以外的所有文件? - How to get all documents but one from firebase firestore in react native? 如何从 firebase firestore 异步获取所有父 collections 的所有文档? - How can I get all the documents of a all parent collections asynchronously from firebase firestore? 如何从 Cloud Firestore 检索多个文档,但没有重复? - How do i retrieve multiple documents from cloud firestore, but no duplicates? 如何从 Firestore 获取多个文档 - How can i get multiple documents from the firestore 如何从 Firestore 获取当前用户的文档? - How can I get documents from Firestore for current user? 如何检索作为文档放置在 Firebase Cloud Firestore 中的 UID - How do I retrieve the UID's that I have placed as documents in my Firebase Cloud Firestore 如何使用Firebase函数从文档中查询数字并将其总和成Firestore中的父文档 - How do a query a number from documents and sum it all up into a parent document in firestore using firebase functions 如何在 Firebase Firestore 的每个文档的子集合中获取文档? - How can I get documents inside a subcollection in every document in Firebase Firestore? 从集合 firebase firestore 中的所有文档中获取数据 - get data from all documents in an collection firebase firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM