Flutter Firestore where clause using map












2















When a new activity is posted i add a new post document into the collection.
Inside this document i have a map where users add confirmation to the event marking it as true and adding his own id.



Firestore Screen



var snap = await Firestore.instance
.collection('user_posts')
.where("confirmations.${user.id}",isEqualTo: true)
.getDocuments();


With this snippet i'm able to get all the posts confirmed by the user. The issue here is to get this a index is required to perform this query. And this index can't be generic. I can't create a index for each user.



Some idea of how to get it?



Thanks!!










share|improve this question





























    2















    When a new activity is posted i add a new post document into the collection.
    Inside this document i have a map where users add confirmation to the event marking it as true and adding his own id.



    Firestore Screen



    var snap = await Firestore.instance
    .collection('user_posts')
    .where("confirmations.${user.id}",isEqualTo: true)
    .getDocuments();


    With this snippet i'm able to get all the posts confirmed by the user. The issue here is to get this a index is required to perform this query. And this index can't be generic. I can't create a index for each user.



    Some idea of how to get it?



    Thanks!!










    share|improve this question



























      2












      2








      2








      When a new activity is posted i add a new post document into the collection.
      Inside this document i have a map where users add confirmation to the event marking it as true and adding his own id.



      Firestore Screen



      var snap = await Firestore.instance
      .collection('user_posts')
      .where("confirmations.${user.id}",isEqualTo: true)
      .getDocuments();


      With this snippet i'm able to get all the posts confirmed by the user. The issue here is to get this a index is required to perform this query. And this index can't be generic. I can't create a index for each user.



      Some idea of how to get it?



      Thanks!!










      share|improve this question
















      When a new activity is posted i add a new post document into the collection.
      Inside this document i have a map where users add confirmation to the event marking it as true and adding his own id.



      Firestore Screen



      var snap = await Firestore.instance
      .collection('user_posts')
      .where("confirmations.${user.id}",isEqualTo: true)
      .getDocuments();


      With this snippet i'm able to get all the posts confirmed by the user. The issue here is to get this a index is required to perform this query. And this index can't be generic. I can't create a index for each user.



      Some idea of how to get it?



      Thanks!!







      firebase flutter google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 20:20









      Frank van Puffelen

      244k29387415




      244k29387415










      asked Jan 2 at 19:56









      KaboomKaboom

      539




      539
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You'll want to turn the confirmations field into an array, and use the (relatively recent) array-contains and arrayUnion operations.



          The equivalent query with an array like that would become:



          var snap = await Firestore.instance
          .collection('user_posts')
          .where("confirmations", arrayContains: user.id)
          .getDocuments();


          And this way you only need an index on confirmations, which is added automatically.



          For more on these see:




          • the blog post introducing these operations

          • the documentation on updating arrays

          • the documentation on array membership queries






          share|improve this answer


























            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%2f54012380%2fflutter-firestore-where-clause-using-map%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














            You'll want to turn the confirmations field into an array, and use the (relatively recent) array-contains and arrayUnion operations.



            The equivalent query with an array like that would become:



            var snap = await Firestore.instance
            .collection('user_posts')
            .where("confirmations", arrayContains: user.id)
            .getDocuments();


            And this way you only need an index on confirmations, which is added automatically.



            For more on these see:




            • the blog post introducing these operations

            • the documentation on updating arrays

            • the documentation on array membership queries






            share|improve this answer






























              1














              You'll want to turn the confirmations field into an array, and use the (relatively recent) array-contains and arrayUnion operations.



              The equivalent query with an array like that would become:



              var snap = await Firestore.instance
              .collection('user_posts')
              .where("confirmations", arrayContains: user.id)
              .getDocuments();


              And this way you only need an index on confirmations, which is added automatically.



              For more on these see:




              • the blog post introducing these operations

              • the documentation on updating arrays

              • the documentation on array membership queries






              share|improve this answer




























                1












                1








                1







                You'll want to turn the confirmations field into an array, and use the (relatively recent) array-contains and arrayUnion operations.



                The equivalent query with an array like that would become:



                var snap = await Firestore.instance
                .collection('user_posts')
                .where("confirmations", arrayContains: user.id)
                .getDocuments();


                And this way you only need an index on confirmations, which is added automatically.



                For more on these see:




                • the blog post introducing these operations

                • the documentation on updating arrays

                • the documentation on array membership queries






                share|improve this answer















                You'll want to turn the confirmations field into an array, and use the (relatively recent) array-contains and arrayUnion operations.



                The equivalent query with an array like that would become:



                var snap = await Firestore.instance
                .collection('user_posts')
                .where("confirmations", arrayContains: user.id)
                .getDocuments();


                And this way you only need an index on confirmations, which is added automatically.



                For more on these see:




                • the blog post introducing these operations

                • the documentation on updating arrays

                • the documentation on array membership queries







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 2 at 21:59









                diegoveloper

                14.1k12534




                14.1k12534










                answered Jan 2 at 20:20









                Frank van PuffelenFrank van Puffelen

                244k29387415




                244k29387415
































                    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%2f54012380%2fflutter-firestore-where-clause-using-map%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

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    Npm cannot find a required file even through it is in the searched directory