【问题标题】:Firebase - Firestore - get key with collection.add()Firebase - Firestore - 使用 collection.add() 获取密钥
【发布时间】:2018-03-26 15:27:40
【问题描述】:

我在使用 Firebase 的新 Firestore 时遇到问题。

情况:我有一个collection('room')

我用collection('room').add(room)创建房间


我要做什么:我需要更新房间。

为此,我使用:collection('room').doc(ROOM_ID).update(update)

所以我需要在我收藏的文档中添加ROOM_ID:

|room
    ROOM_ID
        id:ROOM_ID,
        someContent: ForTheQuery

有没有办法做到这一点?

另一种方法是为自己创建一个生成的 ID:

collection('room')
.doc(someId)
.set({
    id: someId,
    someContent: ForTheQuery
});

但我想避免它。

【问题讨论】:

  • 我认为您提出的解决方案没有问题。你为什么不想那样做呢?
  • 因为我不确定生成ID的功能。我看到每个功能的缺点和优点,所以我不会使用 firebase 生成 iD 保持清洁。
  • 所以您在这里问如何添加带有生成的 Id 的文档,并将该 id 添加为该文档本身的属性?
  • @Doug Stevenson 是的,这是正确的。而且我想避免重复的键。而且我想(这可能是完全错误的,如果没有,请纠正我)Firebase 永远不会有重复的键和一些检查。感谢您的关心。

标签: firebase google-cloud-firestore


【解决方案1】:

您可以使用doc() 来创建对具有唯一ID 的文档的引用,但该文档还不会被创建。然后,您可以使用文档参考中提供的唯一 ID 设置该文档的内容:

const ref = store.collection('users').doc()
console.log(ref.id)  // prints the unique id
ref.set({id: ref.id})  // sets the contents of the doc using the id
.then(() => {  // fetch the doc again and show its data
    ref.get().then(doc => {
        console.log(doc.data())  // prints {id: "the unique id"}
    })
})

【讨论】:

  • 我认为应该注意的是,将 id 设置为 ref doc 中的字段是多余的。我指的是 'ref.set({id: ref.id})' 声明。
  • @Tom 当您需要文档的唯一 ID 参与自动反序列化时,这可以派上用场。因此,如果您有一个 JavaBean 对象,其中文档 ID 应该是该对象的属性之一,这将安排该属性在该对象中自动设置。这避免了为每次反序列化编写额外的代码。
  • 这个 id 可以被另一个并行运行的请求使用吗?我们如何保证这个 id 和正在执行的这个请求的完整性?它是线程安全的吗?
  • 两个函数可以提取相同的 id 吗?在这种情况下,数据完整性也可能受到损害。
  • @LMGagne 为同一集合中的文档生成两个相同 ID 的可能性非常低。如果你担心它,你就是把精力放在了错误的担心上!
【解决方案2】:

角火:

添加数据库前获取ID:

var idBefore =  afs.createId();
    console.log(idBefore );

ANDROID FIRESTORE:

String idBefore = db.collection("YourCol").document().getId();

【讨论】:

【解决方案3】:

您可以使用 collection.ref.add(your item without id) 从创建的文档中获取 ID,响应 (res) 将包含使用其中的 ID 创建的新文档引用。因此,只需执行 res.id 即可获取 ID。

  createOne(options: { item: any, ref: AngularFirestoreCollection<any> }) {
    const promise = new Promise((resolve, reject) => {

      if (options.item) {
        // Convert object to pure javascript
        const item = Object.assign({}, options.item);
        console.log('dataService: createOne: set item: ', item);

        options.ref.ref.add(item)
          .then((res) => {
            console.log('dataService: createOne success: res: ', res);
            resolve(res);
          }).catch(err => {
            console.error('dataService: createOne: error: ', err);
            reject(err);
          });
      } else {
        console.log('dataService: createOne: wrong options! options: ', options);
        reject();
      }
    })

    return promise;
  }

【讨论】:

    【解决方案4】:

    Firebase Javascript SDK:

    只需使用.id 获取密钥,这里是使用async/await 的示例:

      const KEYID = async() => (await fs.collection("testing").add({ data: 'test'})).id;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-17
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多