java - Firestore Java Android Create a structure with collections and nested documents
问题描述
I am creating a post system on an android application I am using Firebase. The posts I have decided that I want to put them on the firestore because it is made to contain large amounts of data. So, I want to use this data structure:
--- posts (collection)
|
--- uid (documents)
|
|
---- postId (documents)
|
--- title: "Post Title"
|
--- date: September 03, 2018 at 6:16:58 PM UTC+3
|
--- valutation: 8
[...] etc
I currently know how to add only one collection with the corresponding data schema, like so:
Map<String, Object> post = new HashMap<>();
post.put("title", title);
post.put("description", description);
post.put("valutation", valutation);
post.put("genre", filmGenre);
post.put("urlImage", urlImage);
post.put("date", dateSeq.toString());
dbRef.collection("posts").add(post);
But this would look like this on the firestore:
--- posts (collection)
|
|
--- title: "Post Title"
|
--- date: September 03, 2018 at 6:16:58 PM UTC+3
|
--- valutation: 8
[...] etc
So I would like to create with java the structure that I showed at the beginning of the post, could someone help me?
解决方案
Collection or sub collection can have document.A document can't hold another document directly.so use below method to place document under "postId" sub collection.
Map<String, Object> post = new HashMap<>();
post.put("title", title);
post.put("description", description);
post.put("valutation", valutation);
post.put("genre", filmGenre);
post.put("urlImage", urlImage);
post.put("date", dateSeq.toString());
//you only need to pass uid and postId
dbRef.collection("posts").document(uid)
.collection(postId).add(post);
推荐阅读
- android - Android - ADB 命令执行拖放操作?
- python-3.x - 从外部子进程模块(pyinstaller)导入内部 exe 模块
- c - 如何在C中用sin和cos画圆?
- python - 允许给定模式之一
- python - 如何根据另一列的状态计算 DATE 的差异?
- airflow - 如何获得 Airflow 作业的最后两个成功执行日期?
- go - 如何在 redisearch-go (Golang) 中为 redigo 客户端提供密码
- c++ - c++中查找模式的函数
- python - 关于 websocket 连接丢失的 Sanic 警报
- node.js - nodejs - TCP 服务器的间歇性 TCP 客户端