Firestore query subcollections

后端 未结 11 1505
余生分开走
余生分开走 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:52

    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.

提交回复
热议问题