如何使用Firestore填充参考字段 [英] How can I populate the reference Field using Firestore

查看:73
本文介绍了如何使用Firestore填充参考字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Do you know how I can populate the reference Field on a Document using Firestore?

推荐答案

创建/获取文档引用时,可以将其保存到另一个文档中.此示例用于Node SDK,但应为您提供有关如何在Android上实现此示例的想法.

When you create / get a document reference, you can save this into another document. This example is for the Node SDK, but it should give you an idea of how to implement this for 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)
  })

这篇关于如何使用Firestore填充参考字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆