简体   繁体   中英

Ionic App using Cloud Firestore - See data for all users

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?

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM