Firebase Firestore - OR query alternative

后端 未结 2 490
陌清茗
陌清茗 2020-12-18 09:38

In this question Firebase Firestore - OR query they said there is no OR query. This means I can\'t easily query for items that have an ID that is in an array I got earlier.

相关标签:
2条回答
  • 2020-12-18 10:11

    You could combine the Observables using rxjs;

    orQuery(){
    
        const $one = this.afs.collection("items", ref => ref.where("itemId","==","1")).valueChanges();
        const $two = this.afs.collection("items", ref => ref.where("itemId","==","3")).valueChanges();
    
        return combineLatest($one,$two).pipe(
            map(([one, two]) => [...one, ...two])
        )
    }
    
    getOr(){
        this.orQuery().subscribe(data => console.log(data))
    }
    
    0 讨论(0)
  • 2020-12-18 10:13

    This seems like a good case for duplicating some of the item data. I assume that some item properties like "name" are immutable and can therefore be stored in the PurchasedItems subcollection without worrying about future updates.

    So if you add some of the data to PurchasedItems:

    Users/uid/PurchasedItems/itemid
    
        // Existing 
        payedInMoney: "$0.00"
        payedInCoins: "100"
        itemId: "itemid"
        timeStamp: timestamp
    
        // Add these
        name: "Item Name"
        url: "http://foo.bar/123"
    

    Then you will have enough information to display the list of a user's purchased items in a single query without needing to also look up each item by ID.

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