【问题标题】:Include the document id as a field id in firestore将文档 ID 作为字段 ID 包含在 Firestore 中
【发布时间】:2020-05-06 11:44:37
【问题描述】:

这就是我想要实现的目标,我希望我的数据库中的每个文档都有一个唯一的 ID 字段,并且我希望该唯一 ID 与文档 ID 相同。

例子:

documents:       data:

eBgdTRE123       id:'eBgdTRE123'
                 name:'Jhon Doe'
                 job:'Programmer'     

我希望我的数据库有这个结构,现在我有两个想法来实现这个

1:使用云功能并拥有onCreate 监听器,每次有新文档抓取文档并设置 id 字段并更新它,这就是我的做法

exports.addDocIdToField = 

functions.firestore.document('collectionname/{docID}').onCreate((snap,context) => {
    const id = context.params.docID;
    return admin.firestore().collection('collectionname')
        .doc(id).set({ id: snap.id }, { merge: true })
        .then(() => {
            return null;
        })
        .catch((error) => {
            return null;
        });
})

2:使用上述方法创建文档。添加文档后立即添加新文档获取新添加的文档并更新其id

它们都有效,但我的问题是我可以依靠这种操作吗?我的意思是如果id 无论如何都是undefined 它可能会在应用程序中进一步导致错误。

或者是否有其他方法可以实现这一点?

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    如果有人对上述提供的答案不满意,
    试试这个 -> docref.set({ 'id':docref.ref.id})。
    它适用于我。下面是这个的一个用例。

    create(tutorial: any): any {
        var docref = this.db.collection('invxxx').doc()
        docref.set({ 'id':docref.ref.id, anotherField: 'anotherValue'});
      }
    

    【讨论】:

      【解决方案2】:

      以前的方法可以正常工作,但只是为了澄清

      它到底是什么样子的

      const { doc, collection, getFirestore, setDoc, addDoc } = require('firebase/firestore');
      let collectionId = "Cars";
      let docId;
      let firestore = getFirestore();
      
      
      
         async function addDocWithId() {
              let collectionRef = collection(firestore, collectionId)
          
              addDoc(collectionRef, {}).then(res => {
                  docId = res.id
                  let docRef = doc(firestore, collectionId + "/" + docId)
          
                  setDoc(docRef, {
                      id: docId,
                      car: 'Benz'
                  })
              })
      
          };
      

      它是如何被澄清的

      const { doc, collection, getFirestore, setDoc, addDoc } = require('firebase/firestore')
      let collectionId = "Cars"
      
      let firestore = getFirestore()
      async function addDocWithId() {
          let collectionRef = collection(firestore, collectionId)
          let docRef = doc(collectionRef)
      
          setDoc(docRef, {
              id: docRef.id,
              car: "Volvo"
          })
      
      
      }
      

      【讨论】:

        【解决方案3】:

        见底部的 JS SDK v9 语法

        有一种更简单的方法可以实现这一点,使用doc() 方法,如下所示(此处使用 JavaScript SDK v8)

        var newDocRef = db.collection('collectionname').doc();
        newDocRef.set({
                         name:'Jhon Doe',
                         job:'Programmer',
                         id: newDocRef.id
                  })
        

        如文档中所述:

        doc() 方法)获取集合中文档的 DocumentReference 指定的路径。 如果没有指定路径,则自动生成 返回的 DocumentReference 将使用唯一 ID。


        您会在其他客户端 SDK 中找到类似的方法,here 用于 Android,here 用于 iOS。


        JS SDK v9 更新:

        import { collection, doc, setDoc } from "firebase/firestore"; 
        
        const newDocRef = doc(collection(db, "collectionname"));
        await setDoc(
               newDocRef, 
               {
                 name:'Jhon Doe',
                 job:'Programmer',
                 id: newDocRef.id
               }
           )
        

        【讨论】:

        • 哦,这样会同时创建文档并设置 id 对吗?
        • 第一行生成一个DocumentReference,带有一个自动生成的唯一ID,第二行只是设置这个文档的字段,并使用自动生成的唯一ID。
        • 所以这将确保每个文档都使用 id 字段生成...非常感谢您
        • Firestore v9 版本??
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-25
        • 2021-01-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多