更新没有 ID 的云 Firestore 文档 [英] update cloud firestore document without id

查看:29
本文介绍了更新没有 ID 的云 Firestore 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Cloud Firestore 如下所示:

My Cloud Firestore looks like this:

users
  ├────random_id_1───{name, email, ...}
  ├────random_id_2───{name, email, ...}
 ...
  └────random_id_n───{name, email, ...}

我想更新 users 的文档,因为我有一个唯一标识符,它不是文档的随机 ID(例如,假设名称是唯一的,我想使用它作为标识符).

I want to update a document of users given I have an unique identifier for it that is NOT the random id of the document (suppose, for example, the name is unique and I want to use it as identifier).

如何更新通过字段标识它的文档?

How can I update a document identifying it by a field of it?

推荐答案

Firestore 只能更新它知道完整引用的文档,这需要文档 ID.在您当前的结构上,您必须运行查询才能找到文档.所以就像:

Firestore can only update documents for which it knows the complete reference, which requires the document ID. On your current structure, you will have to run a query to find the document. So something like:

firebase.firestore().collection("users")
  .where("name", "==", "Daniel")
  .get()
  .then(function(querySnapshot) {
    querySnapshot.forEach(function(document) {
     document.ref.update({ ... }); 
    });
  });

如果您有另一个唯一的属性,我总是建议将其用作文档的 ID.这样一来,您就可以自动保证每个用户只能存在一个文档,而且您不必进行查询来查找文档.

If you have another attribute that is unique, I'd always recommend using that as the IDs for the documents. That way you're automatically guaranteed that only one document per user can exist, and you save yourself having to do a query to find the document.

这篇关于更新没有 ID 的云 Firestore 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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