简体   繁体   English

Firestore按文档ID或文档字段搜索和排序,哪个更好?

[英]Firestore search and order by document id or by document field, which is a better way?

I have two scenarios showing as the following description, and I would like to know that searching or ordering by document id or document field is a better way according to the time costs or other considerations such as firebase pricing.我有两个场景显示如下描述,我想知道根据时间成本或其他考虑因素(例如 firebase 定价),按文档 ID 或文档字段搜索或排序是更好的方法。

  1. A : I set the food name as the document id (assume they are all unique) and each of them has a field called "price", and another architecture B is to make the document a random id and set the food name into the field "name" A : 我将食物名称设置为文档 id(假设它们都是唯一的)并且每个都有一个名为“价格”的字段,另一种架构B是将文档设为随机 id 并将食物名称设置到该字段中“姓名”

    These two kinds of architecture can both be used for searching a certain document according to ID or field, but I would like to know if I am going to search for the price of a certain food, then which kind of architecture is better?这两种架构都可以用于根据ID或字段搜索某个文档,但是我想知道如果我要搜索某个食物的价格,那么哪种架构更好?

 A:   collection "Food":
         document
             |- "apple"  |- "price" : 10
             |- "banana" |- "price" : 13
             |- "lemon"  |- "price" : 15

 B:   collection "Food":
         document
             |- "DXE9JK3V3" |- "name" : "apple"
             |              |- "price" : 10
             |- "VS92S0DV0" |- "name" : "banana"
             |              |- "price" : 13
             |- "VAS0D3JMV" |- "name" : "lemon"
                            |- "price" : 15
  1. The second question is for document ordering (or maybe for filtering), A : I set the document id as event timestamp string, and each of them has a field called "event", and another architecture B is to make the document a random id and make the time data into the field "time"第二个问题是为了文档排序(或者可能是为了过滤), A :我将文档id设置为事件时间戳字符串,每个都有一个名为“事件”的字段,另一个架构B是让文档成为随机id并将时间数据放入“时间”字段中

    I would like to order these documents by the time, according to ID or field, but I don't know which kind of architecture is better?我想按时间排序这些文件,根据ID或字段,但不知道哪种架构更好?

 A:   collection "Event":
         document
             |- "201912201822"  |- "event" : "gym"
             |- "201912130723"  |- "event" : "work"
             |- "201911262247"  |- "event" : "party"

 B:   collection "Event":
         document
             |- "DXE9JK3V3" |- "time" : "2019-12-20 18:22"
             |              |- "event" : "gym"
             |- "VS92S0DV0" |- "time" : "2019-12-13 07:23"
             |              |- "event" : "work"
             |- "VAS0D3JMV" |- "time" : "2019-11-26 22:47"
                            |- "event" : "party"

A: I set the food name as the document id (assume they are all unique) and each of them has a field called "price", and another architecture B is to make the document a random id and set the food name into the field "name" A:我将食物名称设置为文档id(假设它们都是唯一的)并且每个都有一个名为“价格”的字段,另一种架构B是将文档设为随机id并将食物名称设置到字段中“姓名”

In my opinion, B is the option you can go ahead with.在我看来, B是您可以继续的选项。 This means that this approach is more likely to be used if you want your app to massively scale.这意味着如果您希望您的应用程序大规模扩展,则更有可能使用这种方法。 Please also see Dan McGrath's answer from the following post:另请参阅以下帖子中丹·麦格拉思 (Dan McGrath) 的回答:

The second question is for document ordering (or maybe for filtering), A: I set the document id as event timestamp string, and each of them has a field called "event", and another architecture B is to make the document a random id and make the time data into the field "time"第二个问题是为了文档排序(或者可能是为了过滤),A:我把文档id设置为事件时间戳字符串,每个都有一个叫做“event”的字段,另外一个架构B就是让文档变成随机id并将时间数据放入“时间”字段中

Again, B is the option you can go ahead with.同样, B是您可以继续的选项。 To order the results of a query, you can simply pass the direction as the second argument to the orderBy() method:要对查询结果进行排序,您可以简单地将方向作为第二个参数传递给orderBy()方法:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference eventsRef = rootRef.collection("Events");
Query queryEventsByTime = eventsRef.orderBy("time", Query.Direction.ASCENDING);

Or Query.Direction.DESCENDING , according to your needs.Query.Direction.DESCENDING ,根据您的需要。 But be aware that the time property should be of type Date and not of type String, as I see in your schema.但请注意, time属性应该是Date类型而不是String 类型,正如我在您的架构中看到的那样。 To be able to save the date in Firestore, please see my answer from the following post:为了能够在 Firestore 中保存日期,请参阅我在以下帖子中的回答:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM