Perform JOIN query in google cloud firestore

后端 未结 1 1719
庸人自扰
庸人自扰 2021-01-24 17:43
{  
   \"users\":{  
      \"userid_1\":{  
         \"following\":{  
            \"userid_2\":{  
               \"name\":\"user2\"
            },
            \"userid         


        
1条回答
  •  广开言路
    2021-01-24 18:36

    Firestore does not have the concept of a server-side JOIN. All documents in a single read operation must come from the same collection.

    That means that to get data from multiple collections, you will need to perform multiple read operations - at least one per collection, but possibly more. This is normal in most NoSQL databases, and not nearly as slow as many developers think for the amount of data you should read from a client-side app.

    If the number of documents you need to read is prohibitive for your application, consider changing your data model to require fewer reads. Typically this means that you'll end up duplicating some of the data into a format that is more easy to read.

    For example in your use-case you seem to have a social network. A common solution there is to store the complete feed for each user, so all the posts for people they follow, as a separate collection in the database.

    So when a user writes a post, you write that post to the main posts collection, and also to the feed collection of each user that follows them. This operation is known as fanning out your data, and while it complicates the write operation and duplicates data, it makes the code that reads the data simpler, and much more scalable. Since in many applications read operations are far more common than write operations, many NoSQL data modelers consider this a valid trade-off.

    This topic is incredibly broad and hard to do justice in a single answer, which is why I recommend you also:

    • read NoSQL data modeling
    • watch Getting to know Cloud Firestore

    0 讨论(0)
提交回复
热议问题