Firestore query subcollections

后端 未结 11 1574
余生分开走
余生分开走 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条回答
  •  闹比i
    闹比i (楼主)
    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);
        });
    

提交回复
热议问题