[英]How to query normalized firestore database
我有一個 Firestore 數據庫,如下所示:
Root
Accounts
username: UID1
Friends
id: SOME_ID
members: {UID1, UID2}
我在RecyclerView
中顯示所有朋友。 但是如何使用Accounts
集合中的username
段來查詢Friends
列表呢?
使用您當前的數據 model 您必須根據他們的 UID 分別加載每個朋友的用戶配置文件以獲取他們的用戶名。
或者,您可以更改您的數據 model 以在Friends
文檔中也包含每個成員的用戶名。 雖然如果您來自關系數據庫的背景,這種數據重復可能看起來不自然,但它在 NoSQL 數據庫中確實很常見 - 這也是它們在讀取數據時能夠很好地擴展的原因之一。
要了解更多信息,請查看:
app.get("/player/:id", async (req, res) => {
let { id: playerId } = req.params;
if (!playerId) {
res.status(400).send({
success: false,
message: "Kindly provide player ID",
});
return 0;
}
try {
const snapshot = await admin
.firestore()
.collection("players")// players root user
.doc(playerId) // sub folder
.get();
let userData = snapshot.data();
if (userData) {
res.status(200).send({ ...userData });
return 0;
} else {
res.status(400).send({
success: false,
message: "player does not exists in database",
});
}
} catch (err) {
res.status(400).send({ success: false, message: err.message });
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.