Firestore query subcollections

2019-01-01 06:32发布

I thought I read that you can query subcollections with the new Firebase Firestore, but I don't see any examples. For example I have my Firestore setup in the following way:

  • Dances [collection]
    • danceName
    • Songs [collection]
      • songName

How would I be able to query "Find all dances where songName == 'X'"

8条回答
墨雨无痕
2楼-- · 2019-01-01 07:07

As stated by Gil Gilbert, it seems as if collection group queries is currently in the works. In the mean time it is probably better to use root level collections and just link between these collection using the document UID's.

For those who don't already know, Jeff Delaney has some incredible guides and resources for anyone working with Firebase (and Angular) on AngularFirebase.

Firestore NoSQL Relational Data Modeling - Here he breaks down the basics of NoSQL and Firestore DB structuring

Advanced Data Modeling With Firestore by Example - These are more advanced techniques to keep in the back of your mind. A great read for those wanting to take there Firestore skills to the next level

查看更多
情到深处是孤独
3楼-- · 2019-01-01 07:15

Query limitations

Cloud Firestore does not support the following types of queries:

  1. Queries with range filters on different fields.

  2. Single queries across multiple collections or subcollections. Each query runs against a single collection of documents. For more information about how your data structure affects your queries, see Choose a Data Structure.

  3. Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.
  4. Queries with a != clause. In this case, you should split the query into a greater-than query and a less-than query. For example, although the query clause where("age", "!=", "30") is not supported, you can get the same result set by combining two queries, one with the clause where("age", "<", "30") and one with the clause where("age", ">", 30).
查看更多
大哥的爱人
4楼-- · 2019-01-01 07:18
var songs = []    
db.collection('Dances')
      .where('songs.aNameOfASong', '==', true)
      .get()
      .then(function(querySnapshot) {
        var songLength = querySnapshot.size
        var i=0;
        querySnapshot.forEach(function(doc) {
           songs.push(doc.data())
           i ++;
           if(songLength===i){
                console.log(songs
           }
          console.log(doc.id, " => ", doc.data());
        });
       })
       .catch(function(error) {
         console.log("Error getting documents: ", error);
        });
查看更多
查无此人
5楼-- · 2019-01-01 07:21

What if you store songs as an object instead of as a collection? Each dance as, with songs as a field: type Object (not a collection)

{
  danceName: "My Dance",
  songs: {
    "aNameOfASong": true,
    "aNameOfAnotherSong": true,
  }
}

then you could query for all dances with aNameOfASong:

db.collection('Dances')
  .where('songs.aNameOfASong', '==', true)
  .get()
  .then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
      console.log(doc.id, " => ", doc.data());
    });
   })
   .catch(function(error) {
     console.log("Error getting documents: ", error);
    });
查看更多
浮光初槿花落
6楼-- · 2019-01-01 07:22

You can always search like this:-

this.key$ = new BehaviorSubject(null);

return this.key$.switchMap(key =>
  this.angFirestore
    .collection("dances").doc("danceName").collections("songs", ref =>
      ref
        .where("songName", "==", X)
    )
    .snapshotChanges()
    .map(actions => {
      if (actions.toString()) {
        return actions.map(a => {
          const data = a.payload.doc.data() as Dance;
          const id = a.payload.doc.id;
          return { id, ...data };
        });
      } else {
        return false;
      }
    })
);
查看更多
看风景的人
7楼-- · 2019-01-01 07:27

This is a feature which does not yet exist. It's called a "collection group query" and would allow you query all songs regardless of which dance contained them. This is something we intend to support but don't have a concrete timeline on when it's coming.

The alternative structure at this point is to make songs a top-level collection and make which dance the song is a part of a property of the song.

查看更多
登录 后发表回答