Perform JOIN query in google cloud firestore

2019-08-18 00:52发布

{  
   "users":{  
      "userid_1":{  
         "following":{  
            "userid_2":{  
               "name":"user2"
            },
            "userid_3":{  
               "name":"user3"
            }
         }
      },
      "posts":{  
         "postid1":{  
            "createdTime":"111",
            "postedBy":"userid_2"
         },
         "postid2":{  
            "createdTime":"112",
            "postedBy":"userid_3"
         },
         "postid3":{  
            "createdTime":"113",
            "postedBy":"userid_2"
         },
         "postid4":{  
            "createdTime":"114",
            "postedBy":"userid_1"
         }
      }
   }
}

I want to retrieve the posts of "userid_1"'s following users sorted by created time by the limit 2 (2 posts per api call).

How to achieve in fire-store query in node?

Its ok to fetch all the following user's post and sorting the post by created time if following users are less than 100 and they have 10 posts.

If a user have 1000 following people and the 1000 people have 100 posts then its not ok to fetch all the following user's post and sorting by the created time.

I hope we can achieve this easily by SQL "JOIN" query

How to achieve this implementation query in fire-store in node?

1条回答
狗以群分
2楼-- · 2019-08-18 01:16

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:

查看更多
登录 后发表回答