Store and Query Posts in Firestore in a performant wayWhat is firestore Reference data type good for?Working with nested single queries in FirestoreQuery Firestore collection by objectStoring subcollections in Cloud FirestoreIs it possible to query multiple document sub-collections in Cloud Firestore?Firestore data duplicationAccess parent documents field in Firestore rulesFirebase Firestore, query a users friend's postsWay to store Tags for good Query options?How to query Firestore collection for documents with field whose value is contained in a list

How to make a language evolve quickly?

Any suggestions on how to make this table prettier and suggestions on whether it's better to make it fit in 1 or 2 columns

How to get the IP of a user who executed a command?

Why do unstable nuclei form?

What's the difference between const array and static const array in C/C++

How to handle DM constantly stealing everything from sleeping characters?

What can cause an unfrozen indoor copper drain pipe to crack?

Is it a good idea to copy a trader when investing?

Removing all characters except digits from clipboard

Can 'sudo apt-get remove [write]' destroy my Ubuntu?

What does this quote in Small Gods refer to?

Cryptography and elliptic curves

Hexagonal Grid Filling

Can the president of the United States be guilty of insider trading?

Remove color cast in darktable?

date -d 'previous Monday" to display the preceding Monday

Are there variations of the regular runtimes of the Big-O-Notation?

Getting a error after using setState with a promise

We are two immediate neighbors who forged our own powers to form concatenated relationship. Who are we?

What is wrong with my code? RGB potentiometer

Exception propagation

Why do Thanos' punches not kill Captain America or at least cause vital wounds?

What does formal training in a field mean?

Different problems with tabularx



Store and Query Posts in Firestore in a performant way


What is firestore Reference data type good for?Working with nested single queries in FirestoreQuery Firestore collection by objectStoring subcollections in Cloud FirestoreIs it possible to query multiple document sub-collections in Cloud Firestore?Firestore data duplicationAccess parent documents field in Firestore rulesFirebase Firestore, query a users friend's postsWay to store Tags for good Query options?How to query Firestore collection for documents with field whose value is contained in a list






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















So I need to store Posts that are created by Users, now the data modell is the problem, bringing all existing Posts in a Posts Collection with a field of creatorUserID will make it able to show posts belonging to a user.



Now a User has a Subcollection called Followers with the ID of people following, the problem with that is that Im not sure how a query would look to show only Posts of People that the User follows.



Also im worried about performance when there are 10mio+ Posts in the collection.










share|improve this question

















  • 1





    Instead of describing the database structure please add a screenshot of it and indicate the records that you want to get.

    – Alex Mamo
    Mar 23 at 10:03











  • @AlexMamo the structure doesnt exist yet the question is what the most efficient structure would be for such a purpose

    – Badgy
    Mar 23 at 10:20











  • It would be fairly straightforward to read in the uid's stored in the users following collection and then iterate over them to query the posts node for posts containing their user uid's. Also this is a logical OR query and from the docs Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

    – Jay
    Mar 24 at 14:21

















1















So I need to store Posts that are created by Users, now the data modell is the problem, bringing all existing Posts in a Posts Collection with a field of creatorUserID will make it able to show posts belonging to a user.



Now a User has a Subcollection called Followers with the ID of people following, the problem with that is that Im not sure how a query would look to show only Posts of People that the User follows.



Also im worried about performance when there are 10mio+ Posts in the collection.










share|improve this question

















  • 1





    Instead of describing the database structure please add a screenshot of it and indicate the records that you want to get.

    – Alex Mamo
    Mar 23 at 10:03











  • @AlexMamo the structure doesnt exist yet the question is what the most efficient structure would be for such a purpose

    – Badgy
    Mar 23 at 10:20











  • It would be fairly straightforward to read in the uid's stored in the users following collection and then iterate over them to query the posts node for posts containing their user uid's. Also this is a logical OR query and from the docs Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

    – Jay
    Mar 24 at 14:21













1












1








1








So I need to store Posts that are created by Users, now the data modell is the problem, bringing all existing Posts in a Posts Collection with a field of creatorUserID will make it able to show posts belonging to a user.



Now a User has a Subcollection called Followers with the ID of people following, the problem with that is that Im not sure how a query would look to show only Posts of People that the User follows.



Also im worried about performance when there are 10mio+ Posts in the collection.










share|improve this question














So I need to store Posts that are created by Users, now the data modell is the problem, bringing all existing Posts in a Posts Collection with a field of creatorUserID will make it able to show posts belonging to a user.



Now a User has a Subcollection called Followers with the ID of people following, the problem with that is that Im not sure how a query would look to show only Posts of People that the User follows.



Also im worried about performance when there are 10mio+ Posts in the collection.







firebase google-cloud-firestore






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 9:34









BadgyBadgy

1,178422




1,178422







  • 1





    Instead of describing the database structure please add a screenshot of it and indicate the records that you want to get.

    – Alex Mamo
    Mar 23 at 10:03











  • @AlexMamo the structure doesnt exist yet the question is what the most efficient structure would be for such a purpose

    – Badgy
    Mar 23 at 10:20











  • It would be fairly straightforward to read in the uid's stored in the users following collection and then iterate over them to query the posts node for posts containing their user uid's. Also this is a logical OR query and from the docs Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

    – Jay
    Mar 24 at 14:21












  • 1





    Instead of describing the database structure please add a screenshot of it and indicate the records that you want to get.

    – Alex Mamo
    Mar 23 at 10:03











  • @AlexMamo the structure doesnt exist yet the question is what the most efficient structure would be for such a purpose

    – Badgy
    Mar 23 at 10:20











  • It would be fairly straightforward to read in the uid's stored in the users following collection and then iterate over them to query the posts node for posts containing their user uid's. Also this is a logical OR query and from the docs Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

    – Jay
    Mar 24 at 14:21







1




1





Instead of describing the database structure please add a screenshot of it and indicate the records that you want to get.

– Alex Mamo
Mar 23 at 10:03





Instead of describing the database structure please add a screenshot of it and indicate the records that you want to get.

– Alex Mamo
Mar 23 at 10:03













@AlexMamo the structure doesnt exist yet the question is what the most efficient structure would be for such a purpose

– Badgy
Mar 23 at 10:20





@AlexMamo the structure doesnt exist yet the question is what the most efficient structure would be for such a purpose

– Badgy
Mar 23 at 10:20













It would be fairly straightforward to read in the uid's stored in the users following collection and then iterate over them to query the posts node for posts containing their user uid's. Also this is a logical OR query and from the docs Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

– Jay
Mar 24 at 14:21





It would be fairly straightforward to read in the uid's stored in the users following collection and then iterate over them to query the posts node for posts containing their user uid's. Also this is a logical OR query and from the docs Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

– Jay
Mar 24 at 14:21












1 Answer
1






active

oldest

votes


















1














In order to query a document in Firestore the data you want to query by needs to be on the Document you want to query, there is no way of querying a collection by the data of a document from another collection. This is why your use-case is a bit tricky. It might not seem very elegant, but this is a way of solving it:



We use two collections, users and posts.



User
- email: string
- followingUserIDs: array with docIds of users you are following

Posts
- postName: string
- postText: string
- creatorUserID: string


To find all the posts belonging to all the users the logged in user is following, we can do the following in the code:



1 Retrieve the logged in user document



2 For each id in the "followingUserIDs" array I would query Firestore for the Posts. In JavaScript it would be something like:



followingUserIDs.map(followingUserId => 
return firestore.collection('Posts', ref => ref.where('creatorUserID',
'==', followingUserId));
)


3 Combine the result from all the queries into one array of posts






share|improve this answer

























  • But wouldnt this be slow on bigger Amounts of a) following ppl and b) amount of posts in the posts collection?

    – Badgy
    Mar 24 at 15:26











  • Not really because in Firestore query performance is proportional to the size of your result set, not your data set.

    – SnorreDan
    Mar 24 at 15:48











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312401%2fstore-and-query-posts-in-firestore-in-a-performant-way%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In order to query a document in Firestore the data you want to query by needs to be on the Document you want to query, there is no way of querying a collection by the data of a document from another collection. This is why your use-case is a bit tricky. It might not seem very elegant, but this is a way of solving it:



We use two collections, users and posts.



User
- email: string
- followingUserIDs: array with docIds of users you are following

Posts
- postName: string
- postText: string
- creatorUserID: string


To find all the posts belonging to all the users the logged in user is following, we can do the following in the code:



1 Retrieve the logged in user document



2 For each id in the "followingUserIDs" array I would query Firestore for the Posts. In JavaScript it would be something like:



followingUserIDs.map(followingUserId => 
return firestore.collection('Posts', ref => ref.where('creatorUserID',
'==', followingUserId));
)


3 Combine the result from all the queries into one array of posts






share|improve this answer

























  • But wouldnt this be slow on bigger Amounts of a) following ppl and b) amount of posts in the posts collection?

    – Badgy
    Mar 24 at 15:26











  • Not really because in Firestore query performance is proportional to the size of your result set, not your data set.

    – SnorreDan
    Mar 24 at 15:48















1














In order to query a document in Firestore the data you want to query by needs to be on the Document you want to query, there is no way of querying a collection by the data of a document from another collection. This is why your use-case is a bit tricky. It might not seem very elegant, but this is a way of solving it:



We use two collections, users and posts.



User
- email: string
- followingUserIDs: array with docIds of users you are following

Posts
- postName: string
- postText: string
- creatorUserID: string


To find all the posts belonging to all the users the logged in user is following, we can do the following in the code:



1 Retrieve the logged in user document



2 For each id in the "followingUserIDs" array I would query Firestore for the Posts. In JavaScript it would be something like:



followingUserIDs.map(followingUserId => 
return firestore.collection('Posts', ref => ref.where('creatorUserID',
'==', followingUserId));
)


3 Combine the result from all the queries into one array of posts






share|improve this answer

























  • But wouldnt this be slow on bigger Amounts of a) following ppl and b) amount of posts in the posts collection?

    – Badgy
    Mar 24 at 15:26











  • Not really because in Firestore query performance is proportional to the size of your result set, not your data set.

    – SnorreDan
    Mar 24 at 15:48













1












1








1







In order to query a document in Firestore the data you want to query by needs to be on the Document you want to query, there is no way of querying a collection by the data of a document from another collection. This is why your use-case is a bit tricky. It might not seem very elegant, but this is a way of solving it:



We use two collections, users and posts.



User
- email: string
- followingUserIDs: array with docIds of users you are following

Posts
- postName: string
- postText: string
- creatorUserID: string


To find all the posts belonging to all the users the logged in user is following, we can do the following in the code:



1 Retrieve the logged in user document



2 For each id in the "followingUserIDs" array I would query Firestore for the Posts. In JavaScript it would be something like:



followingUserIDs.map(followingUserId => 
return firestore.collection('Posts', ref => ref.where('creatorUserID',
'==', followingUserId));
)


3 Combine the result from all the queries into one array of posts






share|improve this answer















In order to query a document in Firestore the data you want to query by needs to be on the Document you want to query, there is no way of querying a collection by the data of a document from another collection. This is why your use-case is a bit tricky. It might not seem very elegant, but this is a way of solving it:



We use two collections, users and posts.



User
- email: string
- followingUserIDs: array with docIds of users you are following

Posts
- postName: string
- postText: string
- creatorUserID: string


To find all the posts belonging to all the users the logged in user is following, we can do the following in the code:



1 Retrieve the logged in user document



2 For each id in the "followingUserIDs" array I would query Firestore for the Posts. In JavaScript it would be something like:



followingUserIDs.map(followingUserId => 
return firestore.collection('Posts', ref => ref.where('creatorUserID',
'==', followingUserId));
)


3 Combine the result from all the queries into one array of posts







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 14:27

























answered Mar 23 at 12:33









SnorreDanSnorreDan

762412




762412












  • But wouldnt this be slow on bigger Amounts of a) following ppl and b) amount of posts in the posts collection?

    – Badgy
    Mar 24 at 15:26











  • Not really because in Firestore query performance is proportional to the size of your result set, not your data set.

    – SnorreDan
    Mar 24 at 15:48

















  • But wouldnt this be slow on bigger Amounts of a) following ppl and b) amount of posts in the posts collection?

    – Badgy
    Mar 24 at 15:26











  • Not really because in Firestore query performance is proportional to the size of your result set, not your data set.

    – SnorreDan
    Mar 24 at 15:48
















But wouldnt this be slow on bigger Amounts of a) following ppl and b) amount of posts in the posts collection?

– Badgy
Mar 24 at 15:26





But wouldnt this be slow on bigger Amounts of a) following ppl and b) amount of posts in the posts collection?

– Badgy
Mar 24 at 15:26













Not really because in Firestore query performance is proportional to the size of your result set, not your data set.

– SnorreDan
Mar 24 at 15:48





Not really because in Firestore query performance is proportional to the size of your result set, not your data set.

– SnorreDan
Mar 24 at 15:48



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312401%2fstore-and-query-posts-in-firestore-in-a-performant-way%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해