【问题标题】:How can I populate the reference Field using Firestore如何使用 Firestore 填充参考字段
【发布时间】:2018-08-20 20:12:21
【问题描述】:

您知道如何使用 Firestore 填充文档上的参考字段吗?

【问题讨论】:

    标签: android firebase kotlin google-cloud-firestore


    【解决方案1】:

    当您创建/获取文档参考时,您可以将其保存到另一个文档中。此示例适用于 Node SDK,但它应该让您了解如何在 Android 上实现它。

    创建文档参考

    // Create the references
    let myFirstDoc = db.collection('myCollection').doc();
    let mySecondDoc = db.collection('otherCollection').doc();
    
    let batch = db.batch();
    
    // Save the two documents to the batch
    batch.set(myFirstDoc, {someData: true});
    batch.set(mySecondDoc, {firstDocRef: myFirstDoc});
    
    // Commit the batch
    return batch.commit()
      .then(response => {
        console.log('Data saved');
      })
      .catch(err => {
        console.error(err);
      });
    

    获取现有参考

    return db.collection('myCollection').doc('myDocId')
      .then(documentSnapshot => {
        let newDoc = db.collection('otherCollection').add({otherDoc: documentSnapshot.ref});
      })
      .then(response => {
        console.log('Data saved');
      })
      .catch(err => {
        console.error(err)
      })
    

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 2019-01-12
      • 2020-10-06
      • 2018-09-26
      相关资源
      最近更新 更多