在 Firestore 中使用 DocumentReference 和仅使用 id 有什么区别? [英] What's the difference between using a DocumentReference in Firestore and using just the id?

查看:21
本文介绍了在 Firestore 中使用 DocumentReference 和仅使用 id 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Firestore 中,您可以创建数据类型为 Reference 的对象.但这只是所述文档的路径.使用 this 和仅使用 id 作为 String 字段有什么区别?有什么优点/缺点吗?

In Firestore you can create objects with data type Reference. But this is just the path to said document. What's the difference between using this and just using the id as a String field? Any advantages/disadvantages?

推荐答案

Reference 包含文档的完整路径,而简单的字符串 ID 没有上下文.当然,您可以将路径存储为字符串,但为了方便(以及在 自定义对象) 存储整个 Reference 对象会很有用.

A Reference contains the entire path to a document, whereas a simple string ID has no context. Granted, you could just store the path as a string instead, but for convenience (and ease of use in custom objects) it can be useful to have the entire Reference object stored.

Reference 的排序顺序也与 String 的排序顺序不同.来自支持的数据类型文档:

The sort order of a Reference is also different to that of a String. From the Supported Data Types documentation:

  • 参考排序顺序:按路径元素(集合、文档 ID、集合、文档 ID...)
  • 文本字符串排序顺序:UTF-8 编码的字节顺序

这意味着当 编写查询.

例如:

var reference = db.collection("test").document("1");
var query = db.collection("test").orderBy("ref").where("ref", ">", reference);

这篇关于在 Firestore 中使用 DocumentReference 和仅使用 id 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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