Ionic App using Cloud Firestore - See data for all users












0















My data is structure as follows:



collection(people)
document (UserId1)
collection(profiles)
document (generatedId1)
name: 'Speech1'
type: 'admin'
document (UserId2)
collection(profiles)
document (generatedId2)
name: 'Client1'
type: 'client'
collection(tasks)
document (generatedId3)
date: '2018-12-30'
time: 'AM'
severity: 0
document (generatedId4)
date: '2018-12-30'
time: 'PM'
severity: 0
document (UserId3)
collection(profiles)
document (generatedId5)
name: 'Client2'
type: 'client'
collection(tasks)
document (generatedId5)
date: '2018-12-30'
time: 'AM'
severity: 0
document (generatedId6)
date: '2018-12-30'
time: 'PM'
severity: 0


This structure allows me to query the set of tasks for the logged in user with a statement like:






getTasks(){
return new Promise<any>((resolve, reject) => {
let currentUser = firebase.auth().currentUser;
this.snapshotChangesSubscription = this.afs.collection('people').doc(currentUser.uid).collection('tasks').snapshotChanges()
.subscribe(snapshots => {
resolve(snapshots);
})
});
}





However, when I login as an admin type user I would like to see data for all users so I am trying to construct and query that will in essence considered all documents in the people collection.



How can I do this?










share|improve this question





























    0















    My data is structure as follows:



    collection(people)
    document (UserId1)
    collection(profiles)
    document (generatedId1)
    name: 'Speech1'
    type: 'admin'
    document (UserId2)
    collection(profiles)
    document (generatedId2)
    name: 'Client1'
    type: 'client'
    collection(tasks)
    document (generatedId3)
    date: '2018-12-30'
    time: 'AM'
    severity: 0
    document (generatedId4)
    date: '2018-12-30'
    time: 'PM'
    severity: 0
    document (UserId3)
    collection(profiles)
    document (generatedId5)
    name: 'Client2'
    type: 'client'
    collection(tasks)
    document (generatedId5)
    date: '2018-12-30'
    time: 'AM'
    severity: 0
    document (generatedId6)
    date: '2018-12-30'
    time: 'PM'
    severity: 0


    This structure allows me to query the set of tasks for the logged in user with a statement like:






    getTasks(){
    return new Promise<any>((resolve, reject) => {
    let currentUser = firebase.auth().currentUser;
    this.snapshotChangesSubscription = this.afs.collection('people').doc(currentUser.uid).collection('tasks').snapshotChanges()
    .subscribe(snapshots => {
    resolve(snapshots);
    })
    });
    }





    However, when I login as an admin type user I would like to see data for all users so I am trying to construct and query that will in essence considered all documents in the people collection.



    How can I do this?










    share|improve this question



























      0












      0








      0








      My data is structure as follows:



      collection(people)
      document (UserId1)
      collection(profiles)
      document (generatedId1)
      name: 'Speech1'
      type: 'admin'
      document (UserId2)
      collection(profiles)
      document (generatedId2)
      name: 'Client1'
      type: 'client'
      collection(tasks)
      document (generatedId3)
      date: '2018-12-30'
      time: 'AM'
      severity: 0
      document (generatedId4)
      date: '2018-12-30'
      time: 'PM'
      severity: 0
      document (UserId3)
      collection(profiles)
      document (generatedId5)
      name: 'Client2'
      type: 'client'
      collection(tasks)
      document (generatedId5)
      date: '2018-12-30'
      time: 'AM'
      severity: 0
      document (generatedId6)
      date: '2018-12-30'
      time: 'PM'
      severity: 0


      This structure allows me to query the set of tasks for the logged in user with a statement like:






      getTasks(){
      return new Promise<any>((resolve, reject) => {
      let currentUser = firebase.auth().currentUser;
      this.snapshotChangesSubscription = this.afs.collection('people').doc(currentUser.uid).collection('tasks').snapshotChanges()
      .subscribe(snapshots => {
      resolve(snapshots);
      })
      });
      }





      However, when I login as an admin type user I would like to see data for all users so I am trying to construct and query that will in essence considered all documents in the people collection.



      How can I do this?










      share|improve this question
















      My data is structure as follows:



      collection(people)
      document (UserId1)
      collection(profiles)
      document (generatedId1)
      name: 'Speech1'
      type: 'admin'
      document (UserId2)
      collection(profiles)
      document (generatedId2)
      name: 'Client1'
      type: 'client'
      collection(tasks)
      document (generatedId3)
      date: '2018-12-30'
      time: 'AM'
      severity: 0
      document (generatedId4)
      date: '2018-12-30'
      time: 'PM'
      severity: 0
      document (UserId3)
      collection(profiles)
      document (generatedId5)
      name: 'Client2'
      type: 'client'
      collection(tasks)
      document (generatedId5)
      date: '2018-12-30'
      time: 'AM'
      severity: 0
      document (generatedId6)
      date: '2018-12-30'
      time: 'PM'
      severity: 0


      This structure allows me to query the set of tasks for the logged in user with a statement like:






      getTasks(){
      return new Promise<any>((resolve, reject) => {
      let currentUser = firebase.auth().currentUser;
      this.snapshotChangesSubscription = this.afs.collection('people').doc(currentUser.uid).collection('tasks').snapshotChanges()
      .subscribe(snapshots => {
      resolve(snapshots);
      })
      });
      }





      However, when I login as an admin type user I would like to see data for all users so I am trying to construct and query that will in essence considered all documents in the people collection.



      How can I do this?






      getTasks(){
      return new Promise<any>((resolve, reject) => {
      let currentUser = firebase.auth().currentUser;
      this.snapshotChangesSubscription = this.afs.collection('people').doc(currentUser.uid).collection('tasks').snapshotChanges()
      .subscribe(snapshots => {
      resolve(snapshots);
      })
      });
      }





      getTasks(){
      return new Promise<any>((resolve, reject) => {
      let currentUser = firebase.auth().currentUser;
      this.snapshotChangesSubscription = this.afs.collection('people').doc(currentUser.uid).collection('tasks').snapshotChanges()
      .subscribe(snapshots => {
      resolve(snapshots);
      })
      });
      }






      javascript firebase ionic3 google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 18:14









      Alex Mamo

      44.7k82862




      44.7k82862










      asked Jan 1 at 4:36









      user3639858user3639858

      124




      124
























          1 Answer
          1






          active

          oldest

          votes


















          0















          However, when I login as an admin type user I would like to see data for all users so I am trying to construct and query that will in essence considered all documents in the people collection.



          How can I do this?




          Unfortunately, you cannot using this database schema. 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 go. A single query may only use properties of documents in a single collection. So the most simple solution I can think of, would be to add another collection in which you should add all "documents" within all subcollection that exist within all people collection -> uid -> generatedId. So your schema might look similar to this:



          Firestore-root
          |
          --- allDocuments (collection)
          |
          --- generatedId1 (document) //The unique id of the document
          | |
          | --- name: 'Speech1'
          | |
          | --- type: 'admin'
          | |
          | --- uid: 'UserId1'
          |
          --- generatedId 2(document) //The unique id of the document
          |
          --- name: 'Client1'
          |
          --- type: 'client'
          |
          --- uid: 'UserId2'


          In order to display all those users, just attach a listener on the allDocuments reference and get all users objects. If you want to get all admin users, just use a Query that looks like this:



          var allDocumentsRef = db.collection("allDocuments");
          var query = allDocumentsRef.where("type", "==", "admin");





          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%2f53993035%2fionic-app-using-cloud-firestore-see-data-for-all-users%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









            0















            However, when I login as an admin type user I would like to see data for all users so I am trying to construct and query that will in essence considered all documents in the people collection.



            How can I do this?




            Unfortunately, you cannot using this database schema. 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 go. A single query may only use properties of documents in a single collection. So the most simple solution I can think of, would be to add another collection in which you should add all "documents" within all subcollection that exist within all people collection -> uid -> generatedId. So your schema might look similar to this:



            Firestore-root
            |
            --- allDocuments (collection)
            |
            --- generatedId1 (document) //The unique id of the document
            | |
            | --- name: 'Speech1'
            | |
            | --- type: 'admin'
            | |
            | --- uid: 'UserId1'
            |
            --- generatedId 2(document) //The unique id of the document
            |
            --- name: 'Client1'
            |
            --- type: 'client'
            |
            --- uid: 'UserId2'


            In order to display all those users, just attach a listener on the allDocuments reference and get all users objects. If you want to get all admin users, just use a Query that looks like this:



            var allDocumentsRef = db.collection("allDocuments");
            var query = allDocumentsRef.where("type", "==", "admin");





            share|improve this answer




























              0















              However, when I login as an admin type user I would like to see data for all users so I am trying to construct and query that will in essence considered all documents in the people collection.



              How can I do this?




              Unfortunately, you cannot using this database schema. 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 go. A single query may only use properties of documents in a single collection. So the most simple solution I can think of, would be to add another collection in which you should add all "documents" within all subcollection that exist within all people collection -> uid -> generatedId. So your schema might look similar to this:



              Firestore-root
              |
              --- allDocuments (collection)
              |
              --- generatedId1 (document) //The unique id of the document
              | |
              | --- name: 'Speech1'
              | |
              | --- type: 'admin'
              | |
              | --- uid: 'UserId1'
              |
              --- generatedId 2(document) //The unique id of the document
              |
              --- name: 'Client1'
              |
              --- type: 'client'
              |
              --- uid: 'UserId2'


              In order to display all those users, just attach a listener on the allDocuments reference and get all users objects. If you want to get all admin users, just use a Query that looks like this:



              var allDocumentsRef = db.collection("allDocuments");
              var query = allDocumentsRef.where("type", "==", "admin");





              share|improve this answer


























                0












                0








                0








                However, when I login as an admin type user I would like to see data for all users so I am trying to construct and query that will in essence considered all documents in the people collection.



                How can I do this?




                Unfortunately, you cannot using this database schema. 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 go. A single query may only use properties of documents in a single collection. So the most simple solution I can think of, would be to add another collection in which you should add all "documents" within all subcollection that exist within all people collection -> uid -> generatedId. So your schema might look similar to this:



                Firestore-root
                |
                --- allDocuments (collection)
                |
                --- generatedId1 (document) //The unique id of the document
                | |
                | --- name: 'Speech1'
                | |
                | --- type: 'admin'
                | |
                | --- uid: 'UserId1'
                |
                --- generatedId 2(document) //The unique id of the document
                |
                --- name: 'Client1'
                |
                --- type: 'client'
                |
                --- uid: 'UserId2'


                In order to display all those users, just attach a listener on the allDocuments reference and get all users objects. If you want to get all admin users, just use a Query that looks like this:



                var allDocumentsRef = db.collection("allDocuments");
                var query = allDocumentsRef.where("type", "==", "admin");





                share|improve this answer














                However, when I login as an admin type user I would like to see data for all users so I am trying to construct and query that will in essence considered all documents in the people collection.



                How can I do this?




                Unfortunately, you cannot using this database schema. 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 go. A single query may only use properties of documents in a single collection. So the most simple solution I can think of, would be to add another collection in which you should add all "documents" within all subcollection that exist within all people collection -> uid -> generatedId. So your schema might look similar to this:



                Firestore-root
                |
                --- allDocuments (collection)
                |
                --- generatedId1 (document) //The unique id of the document
                | |
                | --- name: 'Speech1'
                | |
                | --- type: 'admin'
                | |
                | --- uid: 'UserId1'
                |
                --- generatedId 2(document) //The unique id of the document
                |
                --- name: 'Client1'
                |
                --- type: 'client'
                |
                --- uid: 'UserId2'


                In order to display all those users, just attach a listener on the allDocuments reference and get all users objects. If you want to get all admin users, just use a Query that looks like this:



                var allDocumentsRef = db.collection("allDocuments");
                var query = allDocumentsRef.where("type", "==", "admin");






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 3 at 18:13









                Alex MamoAlex Mamo

                44.7k82862




                44.7k82862
































                    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%2f53993035%2fionic-app-using-cloud-firestore-see-data-for-all-users%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

                    Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                    Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                    A Topological Invariant for $pi_3(U(n))$