Firestore query subcollections

后端 未结 11 1401
余生分开走
余生分开走 2020-11-22 09:24

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

相关标签:
11条回答
  • 2020-11-22 09:40

    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);
        });
    
    0 讨论(0)
  • 2020-11-22 09:41

    NEW UPDATE July 8, 2019:

    db.collectionGroup('Songs')
      .where('songName', isEqualTo:'X')
      .get()
    
    0 讨论(0)
  • 2020-11-22 09:43

    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).

    0 讨论(0)
  • 2020-11-22 09:49

    Update 2019-05-07

    Today we released collection group queries, and these allow you to query across subcollections.

    So, for example in the web SDK:

    db.collectionGroup('Songs')
      .where('songName', '==', 'X')
      .get()
    

    This would match documents in any collection where the last part of the collection path is 'Songs'.

    Your original question was about finding dances where songName == 'X', and this still isn't possible directly, however, for each Song that matched you can load its parent.

    Original answer

    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.

    0 讨论(0)
  • UPDATE Now Firestore supports array-contains

    Having these documents

        {danceName: 'Danca name 1', songName: ['Title1','Title2']}
        {danceName: 'Danca name 2', songName: ['Title3']}
    

    do it this way

    collection("Dances")
        .where("songName", "array-contains", "Title1")
        .get()...
    

    @Nelson.b.austin Since firestore does not have that yet, I suggest you to have a flat structure, meaning:

    Dances = {
        danceName: 'Dance name 1',
        songName_Title1: true,
        songName_Title2: true,
        songName_Title3: false
    }
    

    Having it in that way, you can get it done:

    var songTitle = 'Title1';
    var dances = db.collection("Dances");
    var query = dances.where("songName_"+songTitle, "==", true);
    

    I hope this helps.

    0 讨论(0)
提交回复
热议问题