在Firestore中使用DocumentReference和仅使用id有什么区别?

编程入门 行业动态 更新时间:2024-10-25 04:18:26
本文介绍了在Firestore中使用DocumentReference和仅使用id有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在Firestore中,您可以创建数据类型为Reference的对象。但这只是说文件的路径。使用这个和使用id作为字符串字段有什么区别?任何优点/缺点?

解决方案

参考文献一个文档,而一个简单的字符串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有什么区别?

本文发布于:2023-11-15 14:27:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   Firestore   DocumentReference   id

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!