0

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.

  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"

    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?

 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"

    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?

 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"
Community
  • 1
  • 1

1 Answers1

0

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"

In my opinion, B is the option you can go ahead with. 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:

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"

Again, B is the option you can go ahead with. To order the results of a query, you can simply pass the direction as the second argument to the orderBy() method:

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. But be aware that the time property should be of type Date and not of type String, as I see in your schema. To be able to save the date in Firestore, please see my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks for your answer. However I am still curious about which one is faster, A or B? For case A, I just need to check the ID, but for case B, I need to look into every document. Therefore, I think case A is faster than caseB. I don't know if this guess is true or false. By the way, firebase will count the reading number for pricing, I am wondering whether these two case have the same "reading count", for case A, If I only check the ID, will firebase also count it as a reading action? – championway Dec 31 '19 at 13:47
  • 1
    Solution B will be much faster since you restrict the results directly from your query. In solution A, you get all documents and filter them client-side since there is no way you can [order documents by their id](https://stackoverflow.com/questions/50010771/order-by-document-id-firestore). Is it ok now? Can I help you with other information? – Alex Mamo Dec 31 '19 at 16:00