How to query to Firestore sub document












0














I'm working on an app and I would like to query Firestore sub document. Let me explain further.




  1. I have a collection of documents where cars are stored, each document has a particular car with description.

  2. In each of those documents above, I av a sub collection called user_data which have it's own document where a particular userid of the user who add the car to his wishlist is stored.


Now I want to get the document of cars if a userid is present in its sub collection. In short, I want to get the wishlist of a particular user.
I'm using streambuilder with listviewbuilder but the problem is how do I perform this query?



Or is there any simpler way of doing this?










share|improve this question





























    0














    I'm working on an app and I would like to query Firestore sub document. Let me explain further.




    1. I have a collection of documents where cars are stored, each document has a particular car with description.

    2. In each of those documents above, I av a sub collection called user_data which have it's own document where a particular userid of the user who add the car to his wishlist is stored.


    Now I want to get the document of cars if a userid is present in its sub collection. In short, I want to get the wishlist of a particular user.
    I'm using streambuilder with listviewbuilder but the problem is how do I perform this query?



    Or is there any simpler way of doing this?










    share|improve this question



























      0












      0








      0







      I'm working on an app and I would like to query Firestore sub document. Let me explain further.




      1. I have a collection of documents where cars are stored, each document has a particular car with description.

      2. In each of those documents above, I av a sub collection called user_data which have it's own document where a particular userid of the user who add the car to his wishlist is stored.


      Now I want to get the document of cars if a userid is present in its sub collection. In short, I want to get the wishlist of a particular user.
      I'm using streambuilder with listviewbuilder but the problem is how do I perform this query?



      Or is there any simpler way of doing this?










      share|improve this question















      I'm working on an app and I would like to query Firestore sub document. Let me explain further.




      1. I have a collection of documents where cars are stored, each document has a particular car with description.

      2. In each of those documents above, I av a sub collection called user_data which have it's own document where a particular userid of the user who add the car to his wishlist is stored.


      Now I want to get the document of cars if a userid is present in its sub collection. In short, I want to get the wishlist of a particular user.
      I'm using streambuilder with listviewbuilder but the problem is how do I perform this query?



      Or is there any simpler way of doing this?







      android firebase dart flutter google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 17:38









      Alex Mamo

      39k72758




      39k72758










      asked Nov 12 at 6:47









      Folarin Opeyemi

      62




      62
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Queries in Firestore are shallow, which means they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and other collections or subcollections in a single query. Firestore doesn't support queries across different collections in one step. So you cannot get items from a collection based on the items that exist within a subcollection. A single query may only use properties of documents in a single collection.




          In short, I want to get the wishlist of a particular user.




          So the most simple solution I can think of, would be to add under each user object an array of favorite cars. Your new database structure should look similar to this:



          Firestore-root
          |
          --- users
          |
          --- uid
          |
          --- favoriteCars : ["carId", "carId"]


          In this way you can query your database to get only the cars a user has marked them as favorite. You can also store instead of those ids in an array, the actual car object. Please see here more details about pros and cons.






          share|improve this answer





















          • Pls I'm developing the app with flutter, how do I combine this with streambuilder. Can u give me a snippet code? I'm thinking of using document reference with the data structure u gave to reference each favourite car to its original document from cars collection
            – Folarin Opeyemi
            Nov 12 at 21:45












          • Yes, that's a good idea. In this case, you should make your own attempt given the information in the answer and ask another question if something else comes up.
            – Alex Mamo
            Nov 13 at 9:25










          • If you think that my answer helped you find the reverse look-up structure that can help you query the database the way you want, please consider accepting my answer by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!
            – Alex Mamo
            Nov 13 at 9:26





















          0














          Currently Firebase Cloud Firestore doesn't support querying with sub collection. You may have to structure your database in way that's possible to query.




          • Store userid in a array in the car document.

          • Use a separate collection to keep the connection between user and cars.


          You can checkout this video from Firebase.



          Maps, Arrays and Subcollections, Oh My! | Get to Know Cloud Firestore






          share|improve this answer





















          • How do I use a separate collection to keep connection between users and cars
            – Folarin Opeyemi
            Nov 12 at 17:02











          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%2f53257112%2fhow-to-query-to-firestore-sub-document%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Queries in Firestore are shallow, which means they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and other collections or subcollections in a single query. Firestore doesn't support queries across different collections in one step. So you cannot get items from a collection based on the items that exist within a subcollection. A single query may only use properties of documents in a single collection.




          In short, I want to get the wishlist of a particular user.




          So the most simple solution I can think of, would be to add under each user object an array of favorite cars. Your new database structure should look similar to this:



          Firestore-root
          |
          --- users
          |
          --- uid
          |
          --- favoriteCars : ["carId", "carId"]


          In this way you can query your database to get only the cars a user has marked them as favorite. You can also store instead of those ids in an array, the actual car object. Please see here more details about pros and cons.






          share|improve this answer





















          • Pls I'm developing the app with flutter, how do I combine this with streambuilder. Can u give me a snippet code? I'm thinking of using document reference with the data structure u gave to reference each favourite car to its original document from cars collection
            – Folarin Opeyemi
            Nov 12 at 21:45












          • Yes, that's a good idea. In this case, you should make your own attempt given the information in the answer and ask another question if something else comes up.
            – Alex Mamo
            Nov 13 at 9:25










          • If you think that my answer helped you find the reverse look-up structure that can help you query the database the way you want, please consider accepting my answer by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!
            – Alex Mamo
            Nov 13 at 9:26


















          0














          Queries in Firestore are shallow, which means they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and other collections or subcollections in a single query. Firestore doesn't support queries across different collections in one step. So you cannot get items from a collection based on the items that exist within a subcollection. A single query may only use properties of documents in a single collection.




          In short, I want to get the wishlist of a particular user.




          So the most simple solution I can think of, would be to add under each user object an array of favorite cars. Your new database structure should look similar to this:



          Firestore-root
          |
          --- users
          |
          --- uid
          |
          --- favoriteCars : ["carId", "carId"]


          In this way you can query your database to get only the cars a user has marked them as favorite. You can also store instead of those ids in an array, the actual car object. Please see here more details about pros and cons.






          share|improve this answer





















          • Pls I'm developing the app with flutter, how do I combine this with streambuilder. Can u give me a snippet code? I'm thinking of using document reference with the data structure u gave to reference each favourite car to its original document from cars collection
            – Folarin Opeyemi
            Nov 12 at 21:45












          • Yes, that's a good idea. In this case, you should make your own attempt given the information in the answer and ask another question if something else comes up.
            – Alex Mamo
            Nov 13 at 9:25










          • If you think that my answer helped you find the reverse look-up structure that can help you query the database the way you want, please consider accepting my answer by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!
            – Alex Mamo
            Nov 13 at 9:26
















          0












          0








          0






          Queries in Firestore are shallow, which means they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and other collections or subcollections in a single query. Firestore doesn't support queries across different collections in one step. So you cannot get items from a collection based on the items that exist within a subcollection. A single query may only use properties of documents in a single collection.




          In short, I want to get the wishlist of a particular user.




          So the most simple solution I can think of, would be to add under each user object an array of favorite cars. Your new database structure should look similar to this:



          Firestore-root
          |
          --- users
          |
          --- uid
          |
          --- favoriteCars : ["carId", "carId"]


          In this way you can query your database to get only the cars a user has marked them as favorite. You can also store instead of those ids in an array, the actual car object. Please see here more details about pros and cons.






          share|improve this answer












          Queries in Firestore are shallow, which means they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and other collections or subcollections in a single query. Firestore doesn't support queries across different collections in one step. So you cannot get items from a collection based on the items that exist within a subcollection. A single query may only use properties of documents in a single collection.




          In short, I want to get the wishlist of a particular user.




          So the most simple solution I can think of, would be to add under each user object an array of favorite cars. Your new database structure should look similar to this:



          Firestore-root
          |
          --- users
          |
          --- uid
          |
          --- favoriteCars : ["carId", "carId"]


          In this way you can query your database to get only the cars a user has marked them as favorite. You can also store instead of those ids in an array, the actual car object. Please see here more details about pros and cons.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 17:37









          Alex Mamo

          39k72758




          39k72758












          • Pls I'm developing the app with flutter, how do I combine this with streambuilder. Can u give me a snippet code? I'm thinking of using document reference with the data structure u gave to reference each favourite car to its original document from cars collection
            – Folarin Opeyemi
            Nov 12 at 21:45












          • Yes, that's a good idea. In this case, you should make your own attempt given the information in the answer and ask another question if something else comes up.
            – Alex Mamo
            Nov 13 at 9:25










          • If you think that my answer helped you find the reverse look-up structure that can help you query the database the way you want, please consider accepting my answer by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!
            – Alex Mamo
            Nov 13 at 9:26




















          • Pls I'm developing the app with flutter, how do I combine this with streambuilder. Can u give me a snippet code? I'm thinking of using document reference with the data structure u gave to reference each favourite car to its original document from cars collection
            – Folarin Opeyemi
            Nov 12 at 21:45












          • Yes, that's a good idea. In this case, you should make your own attempt given the information in the answer and ask another question if something else comes up.
            – Alex Mamo
            Nov 13 at 9:25










          • If you think that my answer helped you find the reverse look-up structure that can help you query the database the way you want, please consider accepting my answer by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!
            – Alex Mamo
            Nov 13 at 9:26


















          Pls I'm developing the app with flutter, how do I combine this with streambuilder. Can u give me a snippet code? I'm thinking of using document reference with the data structure u gave to reference each favourite car to its original document from cars collection
          – Folarin Opeyemi
          Nov 12 at 21:45






          Pls I'm developing the app with flutter, how do I combine this with streambuilder. Can u give me a snippet code? I'm thinking of using document reference with the data structure u gave to reference each favourite car to its original document from cars collection
          – Folarin Opeyemi
          Nov 12 at 21:45














          Yes, that's a good idea. In this case, you should make your own attempt given the information in the answer and ask another question if something else comes up.
          – Alex Mamo
          Nov 13 at 9:25




          Yes, that's a good idea. In this case, you should make your own attempt given the information in the answer and ask another question if something else comes up.
          – Alex Mamo
          Nov 13 at 9:25












          If you think that my answer helped you find the reverse look-up structure that can help you query the database the way you want, please consider accepting my answer by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!
          – Alex Mamo
          Nov 13 at 9:26






          If you think that my answer helped you find the reverse look-up structure that can help you query the database the way you want, please consider accepting my answer by clicking the checkmark (✔️) on the left side under the vote arrows. Should change the color in green. I'd appreciate it. Thanks!
          – Alex Mamo
          Nov 13 at 9:26















          0














          Currently Firebase Cloud Firestore doesn't support querying with sub collection. You may have to structure your database in way that's possible to query.




          • Store userid in a array in the car document.

          • Use a separate collection to keep the connection between user and cars.


          You can checkout this video from Firebase.



          Maps, Arrays and Subcollections, Oh My! | Get to Know Cloud Firestore






          share|improve this answer





















          • How do I use a separate collection to keep connection between users and cars
            – Folarin Opeyemi
            Nov 12 at 17:02
















          0














          Currently Firebase Cloud Firestore doesn't support querying with sub collection. You may have to structure your database in way that's possible to query.




          • Store userid in a array in the car document.

          • Use a separate collection to keep the connection between user and cars.


          You can checkout this video from Firebase.



          Maps, Arrays and Subcollections, Oh My! | Get to Know Cloud Firestore






          share|improve this answer





















          • How do I use a separate collection to keep connection between users and cars
            – Folarin Opeyemi
            Nov 12 at 17:02














          0












          0








          0






          Currently Firebase Cloud Firestore doesn't support querying with sub collection. You may have to structure your database in way that's possible to query.




          • Store userid in a array in the car document.

          • Use a separate collection to keep the connection between user and cars.


          You can checkout this video from Firebase.



          Maps, Arrays and Subcollections, Oh My! | Get to Know Cloud Firestore






          share|improve this answer












          Currently Firebase Cloud Firestore doesn't support querying with sub collection. You may have to structure your database in way that's possible to query.




          • Store userid in a array in the car document.

          • Use a separate collection to keep the connection between user and cars.


          You can checkout this video from Firebase.



          Maps, Arrays and Subcollections, Oh My! | Get to Know Cloud Firestore







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 7:41









          UdeshUK

          400413




          400413












          • How do I use a separate collection to keep connection between users and cars
            – Folarin Opeyemi
            Nov 12 at 17:02


















          • How do I use a separate collection to keep connection between users and cars
            – Folarin Opeyemi
            Nov 12 at 17:02
















          How do I use a separate collection to keep connection between users and cars
          – Folarin Opeyemi
          Nov 12 at 17:02




          How do I use a separate collection to keep connection between users and cars
          – Folarin Opeyemi
          Nov 12 at 17:02


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53257112%2fhow-to-query-to-firestore-sub-document%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

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values