javascript - 是否可以在两个不同的集合中移动并根据 Firebase 中的 ID 组合来自它们的信息
问题描述
我正在制作一个以 firebase 作为数据库的 Web 应用程序,但是在使用 javascript 编程时遇到了问题。我有两组不同的集合,其中包含信息。为了避免重复,我需要将它们组合起来以从中获取数据。为此,我继续在其中一个集合中创建了 ID,以将其与另一个集合相关联。
这是我从另一个堆栈溢出 链接到该论坛的一些代码
db.collection("articlesCollection")
.doc(articleId)
.get()
.then(article => {
firebase.firestore().collection("commentsCollection")
.where('articleRef.' + article.id, '==', true)
.get()
.then(results => {
results.forEach(function (comment) {
console.log(comment.id, " => ", comment.data());
});
});
});
这是上面的代码,我试图重新调整用途,但在不太了解其结构如何工作后最终停止。
db.collection("Users").get().then(article => {
firebase.firestore().collection("trips").where('Users.' + doc.id, '==', ownersID)
.get()
.then(results => {
//got lost here. I need the forEach function, but
results.forEach(function (comment) {
console.log(comment.id, " => ", comment.data());
//eventually change these into variables to display in a
//form.
});
});
});
下面将简要介绍我的数据库结构。还有一些信息,我将使用 () 来描述正在发生的事情。
Trips\uniqueID(generated by Google)\ownerID(& other info)
Users\ownerID\info.
如您所见,ownerID 位于两组集合中。我需要从 trips 集合中获取关于他们要去哪里的信息。我需要来自用户集合的信息,因为它必须包含所有者信息,例如姓名和电子邮件。
经过研究和反复试验,我发现这可能对此有所帮助。
这是我以前的。希望它能提供更多关于我试图用我的 javascript 做什么的见解。
db.collection("trips").where("flag", "==", true)
.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
//this info needs to be taken from the Users collection
var fiName = doc.data().fName;
var laName = doc.data().lName;
var fullName = fiName + " " + laName;
var wscEmail = doc.data().wscEmail;
var phoneNum = doc.data().phoneNumber;
//Information that I can take from the trips table.
var id = doc.data().ownerID;
var idName = doc.id;
var destination = doc.data().destination;
var location = doc.data().location;
var date = doc.data().dateGoing;
var arrivalTime = doc.data().arrivalTime;
var departureTime = doc.data().departureTime;
var returnTime = doc.data().returnTime;
var seats = doc.data().seats;
//leave this for now. Gopal is working on it
//var email = "temp@Email";
//This is something we can do, but we can also leave it
//var phoneNum = "(712) 111-2211";
var description = doc.data().tripDescription;
feed.innerHTML += " \
<div'>\
<div class='post'>\
<div>\
<img src='circleProfile.png' alt='User photo'>\
<div class='name'> <p>" + fullName + "</p></div>\
<div>Destination: <span class='postInfo'>" + destination + "</span></div>\
<div>Location: <span class='postInfo'>" + location + "</span></div>\
<div>Date: <span class='postInfo'>" + date + "</span></div>\
<table>\
<tr>\
<th>Departure</th>\
<th>Arrival</th>\
<th>Return</th>\
<th>Seats</th>\
</tr>\
<tr>\
<td>" + departureTime + "</td>\
<td>" + arrivalTime + "</td>\
<td>" + returnTime + "</td>\
<td>" + seats + "</td>\
</tr>\
</table>\
<div>\
<p>Email: " + wscEmail + " <span class='postInfo'>" + phoneNum + "</span></p>\
</div>\
<div class='description'><p>" + description + "</p></div>\
<div class='spaceWars' onclick='deletePost(this.id)' id=" + idName + "> <button> Click me</button> </div>\
</div>\
</div>\
</div>";
});
});
});
如您所见,我需要来自另一个集合的信息才能出现在此表中。问题是我不知道如何合并其他集合。
旁注:这是我第一次使用非关系数据库,所以它背后的格式和结构一直让我感到厌烦。还有一个事实是,firebase 还没有一些功能,这使得这样做有点困难。
感谢您的阅读,我知道这很多,如果您完成所有这些并了解我在做什么,我将不胜感激。希望这将为将来的其他人提供一些见解。
解决方案
NoSQL 世界中的一种常见方法是在遇到您在问题中描述的情况时复制数据。数据的存储并不昂贵,您应该以优化查询而不一定优化存储的方式对数据库进行建模(通过像在 SQL DB 中那样进行标准化)。
换句话说,您将在集合下的文档中保存fName
、lName
、和值。laName
wscEmail
phoneNumber
Trips
你会在网上找到很多关于这种方法的“文献”,例如https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/。您还可以观看以下视频https://www.youtube.com/watch?v=vKqXSZLLnHA(用于实时数据库,但也适用于 Firestore)。
推荐阅读
- kubernetes - 如何在“livenessProbe”中放置两个prot?
- oracle - 无法为模块 DBD::Oracle 加载 Oracle.dll:load_file:%1 在 C:/Perl64/lib/DynaLoader.pm 第 201 行不是有效的 Win32 应用程序
- ios - 动态分配 ViewController 到 Navigate
- python - Django admin inline 有 2 个外键指向同一模型
- encryption - 用于加密的 Webauthn
- tensorflow - 将中间层的输出作为模型训练的输入
- matlab - 如何访问和修改 matlab 工作区中的变量值?
- python - 如何在谷歌colab中安装web3
- javascript - 如何定义包含泛型类型变量的类型数组
- wordpress - Wordpress REST 响应仅显示带有 ID 的分类类别