如何使用Firestore填充引用字段

Igo*_*ner 2 android kotlin firebase google-cloud-firestore

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

公司的FireStore

Jas*_*man 5

创建/获取文档引用时,可以将其保存到另一个文档中.此示例适用于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);
  });
Run Code Online (Sandbox Code Playgroud)

获得现有参考

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)
  })
Run Code Online (Sandbox Code Playgroud)