答案 0 :(得分: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);
});
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)
})