更新不带ID的Cloud Firestore文档 [英] update cloud firestore document without id

查看:77
本文介绍了更新不带ID的Cloud 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(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的Cloud Firestore文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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