firebase - firestore参考数据类型有什么用处?

original title: "firebase - What is firestore Reference data type good for?"


Translate

I'm just exploring the new Firebase Firestore and it contains a data type called reference. It is not clear to me what this does.

  • Is it like foreign key?
  • Can it be used to point to a collection that is located somewhere else?
  • If reference is an actual reference, can I use it for queries? For example can I have a reference that points directly to the user, instead of storing the userId in a text field? And can I use this user reference for querying?


我只是在探索新的Firebase Firestore,它包含一个称为引用的数据类型。我不清楚这是做什么的。像外键吗?可以用来指向收藏夹吗?

这是翻译后的摘要,如果您需要查看完整的翻译,请单击“Translate”图标


所有的回答
  • Translate

    References are very much like foreign keys.

    The currently released SDKs cannot store references to other projects. Within a project, references can point to any other document in any other collection.

    You can use references in queries like any other value: for filtering, ordering, and for paging (startAt/startAfter).

    Unlike foreign keys in a SQL database, references are not useful for performing joins in a single query. You can use them for dependent lookups (which seem join like), but be careful because each hop will result in another round trip to the server.


  • Translate

    Adding below what worked for me using references in Firestore.

    As the other answers say, it's like a foreign key. The reference attribute doesn't return the data of the reference doc though. For example, I have a list of products, with a userRef reference as one of the attributes on the product. Getting the list of products, gives me the reference of the user that created that product. But it doesn't give me the details of the user in that reference. I've used other back end as a services with pointers before that have a "populate: true" flag that gives the user details back instead of just the reference id of the user, which would be great to have here (hopefully a future improvement).

    Below is some example code that I used to set the reference as well as get the collection of products list then get the user details from the user reference id given.

    Set a reference on a collection:

    let data = {
      name: 'productName',
      size: 'medium',
      userRef: db.doc('users/' + firebase.auth().currentUser.uid)
    };
    db.collection('products').add(data);
    

    Get a collection (products) and all references on each document (user details):

    db.collection('products').get()
        .then(res => {
          vm.mainListItems = [];
          res.forEach(doc => {
            let newItem = doc.data();
            newItem.id = doc.id;
            if (newItem.userRef) {
              newItem.userRef.get()
              .then(res => { 
                newItem.userData = res.data() 
                vm.mainListItems.push(newItem);
              })
              .catch(err => console.error(err));
            } else {
              vm.mainListItems.push(newItem);  
            }
    
          });
        })
        .catch(err => { console.error(err) });
    

    Hope this helps


  • Translate

    For those looking for a Javascript solution to querying by reference - the concept is that, you need to use a 'document reference' object in the query statement

    teamDbRef = db.collection('teams').doc('CnbasS9cZQ2SfvGY2r3b'); /* CnbasS9cZQ2SfvGY2r3b being the collection ID */
    //
    //
    db.collection("squad").where('team', '==', teamDbRef).get().then((querySnapshot) => {
      //
    }).catch(function(error) {
      //
    });
    

    (Kudos to the answer here: https://stackoverflow.com/a/53141199/1487867)