参考文献一个文档,而一个简单的字符串ID没有上下文。当然,您可以将路径存储为一个字符串,但为了方便(在自定义对象)将整个 Reference引用对象存储是非常有用的。
引用的排序顺序也不同于 String 的排序顺序。从支持的数据类型文档:
- 引用排序顺序:按路径元素(集合,文档ID,集合,文档ID ...) b $ b
- 文本字符串排序顺序:UTF-8编码的字节顺序
您还可以在参考对象进行比较query-data / queriesrel =nofollow noreferrer>编写查询。 $ b
例如: document(1); var query = db.collection(test)。orderBy(ref)。where(ref,>,reference);
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?
解决方案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.
The sort order of a Reference is also different to that of a String. From the Supported Data Types documentation:
- Reference sort order: By path elements (collection, document ID, collection, document ID...)
- Text string sort order: UTF-8 encoded byte order
This means that you can also filter on a Reference object in the database by comparing it to another when writing queries.
For example:
var reference = db.collection("test").document("1"); var query = db.collection("test").orderBy("ref").where("ref", ">", reference);
更多推荐
在Firestore中使用DocumentReference和仅使用id有什么区别?
发布评论