Way to store Tags for good Query options?












0















I have a Collection of Posts and a Collection of Users. Posts have certain attributes which are irrelevant for this, but they also have one attribute called tags right now its an array with certain words.



A User can follow certain Tags so he has a attribute followedTags which is also an Array right now that contains the Tags he follows.



Now one of the use cases for this is a User Feed to Show only Posts with Tags he follows, the Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays). Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags) I thought maybe I could make a smart change in the data modell itself, any suggestions?










share|improve this question



























    0















    I have a Collection of Posts and a Collection of Users. Posts have certain attributes which are irrelevant for this, but they also have one attribute called tags right now its an array with certain words.



    A User can follow certain Tags so he has a attribute followedTags which is also an Array right now that contains the Tags he follows.



    Now one of the use cases for this is a User Feed to Show only Posts with Tags he follows, the Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays). Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags) I thought maybe I could make a smart change in the data modell itself, any suggestions?










    share|improve this question

























      0












      0








      0








      I have a Collection of Posts and a Collection of Users. Posts have certain attributes which are irrelevant for this, but they also have one attribute called tags right now its an array with certain words.



      A User can follow certain Tags so he has a attribute followedTags which is also an Array right now that contains the Tags he follows.



      Now one of the use cases for this is a User Feed to Show only Posts with Tags he follows, the Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays). Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags) I thought maybe I could make a smart change in the data modell itself, any suggestions?










      share|improve this question














      I have a Collection of Posts and a Collection of Users. Posts have certain attributes which are irrelevant for this, but they also have one attribute called tags right now its an array with certain words.



      A User can follow certain Tags so he has a attribute followedTags which is also an Array right now that contains the Tags he follows.



      Now one of the use cases for this is a User Feed to Show only Posts with Tags he follows, the Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays). Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags) I thought maybe I could make a smart change in the data modell itself, any suggestions?







      firebase google-cloud-firestore






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 10:51









      BadgyBadgy

      1,111420




      1,111420
























          1 Answer
          1






          active

          oldest

          votes


















          1















          Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays)




          If you try to chain multiple whereArrayContains() methods, you're most likely getting the following error:




          Caused by: java.lang.IllegalArgumentException: Invalid Query. Queries only support having a single array-contains filter.




          So unfortunately Firestore can only allow a single call to whereArrayContains() method in query.




          Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags)




          If you have a reasonable number tags, you can create each tag as a separate property. In this case, it is allowed to call Firestore Query's whereEqualTo() multiple times.



          If this is not the case, then you should consider augmenting your database structure to allow a reverse lookup by creating each tag as a seprate object (document) in a tagCollection. Under each document you can create a new collection named tagPost in which you should add all the posts that are labeled with a specific tag.






          share|improve this answer
























          • Even if I would use your solution on the bottom, how would I query only for Posts that contain those 2 Tags? It gets really messy then and I would prefer a simpler solution

            – Badgy
            Nov 22 '18 at 12:42











          • Using my answer from this post. If it is for Android, you create two separate task objects and then pass them to the whenAllSuccess() method :) If it is for other programming language, you should check the docs.

            – Alex Mamo
            Nov 22 '18 at 12:45













          • Does something like that exist in JS?

            – Badgy
            Nov 22 '18 at 12:46











          • It should be. Check the docs.

            – Alex Mamo
            Nov 22 '18 at 12:47











          • Do you think that my answer/comment helped you regarding the initial question?

            – Alex Mamo
            Nov 22 '18 at 12:47













          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%2f53429304%2fway-to-store-tags-for-good-query-options%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















          Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays)




          If you try to chain multiple whereArrayContains() methods, you're most likely getting the following error:




          Caused by: java.lang.IllegalArgumentException: Invalid Query. Queries only support having a single array-contains filter.




          So unfortunately Firestore can only allow a single call to whereArrayContains() method in query.




          Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags)




          If you have a reasonable number tags, you can create each tag as a separate property. In this case, it is allowed to call Firestore Query's whereEqualTo() multiple times.



          If this is not the case, then you should consider augmenting your database structure to allow a reverse lookup by creating each tag as a seprate object (document) in a tagCollection. Under each document you can create a new collection named tagPost in which you should add all the posts that are labeled with a specific tag.






          share|improve this answer
























          • Even if I would use your solution on the bottom, how would I query only for Posts that contain those 2 Tags? It gets really messy then and I would prefer a simpler solution

            – Badgy
            Nov 22 '18 at 12:42











          • Using my answer from this post. If it is for Android, you create two separate task objects and then pass them to the whenAllSuccess() method :) If it is for other programming language, you should check the docs.

            – Alex Mamo
            Nov 22 '18 at 12:45













          • Does something like that exist in JS?

            – Badgy
            Nov 22 '18 at 12:46











          • It should be. Check the docs.

            – Alex Mamo
            Nov 22 '18 at 12:47











          • Do you think that my answer/comment helped you regarding the initial question?

            – Alex Mamo
            Nov 22 '18 at 12:47


















          1















          Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays)




          If you try to chain multiple whereArrayContains() methods, you're most likely getting the following error:




          Caused by: java.lang.IllegalArgumentException: Invalid Query. Queries only support having a single array-contains filter.




          So unfortunately Firestore can only allow a single call to whereArrayContains() method in query.




          Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags)




          If you have a reasonable number tags, you can create each tag as a separate property. In this case, it is allowed to call Firestore Query's whereEqualTo() multiple times.



          If this is not the case, then you should consider augmenting your database structure to allow a reverse lookup by creating each tag as a seprate object (document) in a tagCollection. Under each document you can create a new collection named tagPost in which you should add all the posts that are labeled with a specific tag.






          share|improve this answer
























          • Even if I would use your solution on the bottom, how would I query only for Posts that contain those 2 Tags? It gets really messy then and I would prefer a simpler solution

            – Badgy
            Nov 22 '18 at 12:42











          • Using my answer from this post. If it is for Android, you create two separate task objects and then pass them to the whenAllSuccess() method :) If it is for other programming language, you should check the docs.

            – Alex Mamo
            Nov 22 '18 at 12:45













          • Does something like that exist in JS?

            – Badgy
            Nov 22 '18 at 12:46











          • It should be. Check the docs.

            – Alex Mamo
            Nov 22 '18 at 12:47











          • Do you think that my answer/comment helped you regarding the initial question?

            – Alex Mamo
            Nov 22 '18 at 12:47
















          1












          1








          1








          Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays)




          If you try to chain multiple whereArrayContains() methods, you're most likely getting the following error:




          Caused by: java.lang.IllegalArgumentException: Invalid Query. Queries only support having a single array-contains filter.




          So unfortunately Firestore can only allow a single call to whereArrayContains() method in query.




          Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags)




          If you have a reasonable number tags, you can create each tag as a separate property. In this case, it is allowed to call Firestore Query's whereEqualTo() multiple times.



          If this is not the case, then you should consider augmenting your database structure to allow a reverse lookup by creating each tag as a seprate object (document) in a tagCollection. Under each document you can create a new collection named tagPost in which you should add all the posts that are labeled with a specific tag.






          share|improve this answer














          Problem is that I only found methods to query this for ONE Attribute at a time(in Arrays)




          If you try to chain multiple whereArrayContains() methods, you're most likely getting the following error:




          Caused by: java.lang.IllegalArgumentException: Invalid Query. Queries only support having a single array-contains filter.




          So unfortunately Firestore can only allow a single call to whereArrayContains() method in query.




          Since I dont wanna run 20 Querys for 1 Feed (For example if the user follows 20 Tags)




          If you have a reasonable number tags, you can create each tag as a separate property. In this case, it is allowed to call Firestore Query's whereEqualTo() multiple times.



          If this is not the case, then you should consider augmenting your database structure to allow a reverse lookup by creating each tag as a seprate object (document) in a tagCollection. Under each document you can create a new collection named tagPost in which you should add all the posts that are labeled with a specific tag.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 11:08









          Alex MamoAlex Mamo

          44.6k82862




          44.6k82862













          • Even if I would use your solution on the bottom, how would I query only for Posts that contain those 2 Tags? It gets really messy then and I would prefer a simpler solution

            – Badgy
            Nov 22 '18 at 12:42











          • Using my answer from this post. If it is for Android, you create two separate task objects and then pass them to the whenAllSuccess() method :) If it is for other programming language, you should check the docs.

            – Alex Mamo
            Nov 22 '18 at 12:45













          • Does something like that exist in JS?

            – Badgy
            Nov 22 '18 at 12:46











          • It should be. Check the docs.

            – Alex Mamo
            Nov 22 '18 at 12:47











          • Do you think that my answer/comment helped you regarding the initial question?

            – Alex Mamo
            Nov 22 '18 at 12:47





















          • Even if I would use your solution on the bottom, how would I query only for Posts that contain those 2 Tags? It gets really messy then and I would prefer a simpler solution

            – Badgy
            Nov 22 '18 at 12:42











          • Using my answer from this post. If it is for Android, you create two separate task objects and then pass them to the whenAllSuccess() method :) If it is for other programming language, you should check the docs.

            – Alex Mamo
            Nov 22 '18 at 12:45













          • Does something like that exist in JS?

            – Badgy
            Nov 22 '18 at 12:46











          • It should be. Check the docs.

            – Alex Mamo
            Nov 22 '18 at 12:47











          • Do you think that my answer/comment helped you regarding the initial question?

            – Alex Mamo
            Nov 22 '18 at 12:47



















          Even if I would use your solution on the bottom, how would I query only for Posts that contain those 2 Tags? It gets really messy then and I would prefer a simpler solution

          – Badgy
          Nov 22 '18 at 12:42





          Even if I would use your solution on the bottom, how would I query only for Posts that contain those 2 Tags? It gets really messy then and I would prefer a simpler solution

          – Badgy
          Nov 22 '18 at 12:42













          Using my answer from this post. If it is for Android, you create two separate task objects and then pass them to the whenAllSuccess() method :) If it is for other programming language, you should check the docs.

          – Alex Mamo
          Nov 22 '18 at 12:45







          Using my answer from this post. If it is for Android, you create two separate task objects and then pass them to the whenAllSuccess() method :) If it is for other programming language, you should check the docs.

          – Alex Mamo
          Nov 22 '18 at 12:45















          Does something like that exist in JS?

          – Badgy
          Nov 22 '18 at 12:46





          Does something like that exist in JS?

          – Badgy
          Nov 22 '18 at 12:46













          It should be. Check the docs.

          – Alex Mamo
          Nov 22 '18 at 12:47





          It should be. Check the docs.

          – Alex Mamo
          Nov 22 '18 at 12:47













          Do you think that my answer/comment helped you regarding the initial question?

          – Alex Mamo
          Nov 22 '18 at 12:47







          Do you think that my answer/comment helped you regarding the initial question?

          – Alex Mamo
          Nov 22 '18 at 12:47






















          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%2f53429304%2fway-to-store-tags-for-good-query-options%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

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]