这里有一个提案:
一个名为photos
的集合
其中包含具有以下内容的photo
文档:
一个名为users
的集合
其中包含具有以下内容的user
文档:
- 用户的ID(即来自身份验证的
uid
)
- 一个
followedUsers
对象,保存当前用户关注的用户ID {userId1: true, userId2: true}
- 一个
followedPhotosId
对象,保存当前用户关注的照片ID {9HzWzyXmcnBDuhTZKoQw: true, fO0OvzJs9M8p9N0jufte: true}
- 一个
followedPhotos
子集合,保存关注的照片的详细信息
以下HTML页面展示了如何执行您在帖子中列出的查询,以及一些读写数据的查询:这些最新的查询应该用于几个云函数,专门用于在用户关注新用户和/或被一个或多个用户关注的用户添加或删除照片时保持followedPhotosId
对象和followedPhotos
集合同步。
<html>
<head>
<script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-functions.js"></script>
</head>
<body>
<script>
var config = {
apiKey: "",
authDomain: "xxxxx.firebaseapp.com",
databaseURL: "https://xxxx.firebaseio.com",
projectId: "xxxxxx"
};
firebase.initializeApp(config);
var firestoredb = firebase.firestore();
firestoredb.collection("photos").orderBy("creation", "desc")
.get()
.then(function(querySnapshot) {
console.log("YOUR QUERY #1");
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
firestoredb.collection("users").doc("user1").collection("followedPhotos")
.get()
.then(function(querySnapshot) {
console.log("YOUR QUERY #2");
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
firestoredb.collection("photos")
.where('tags.cats', '==', true)
.get()
.then(function(querySnapshot) {
console.log("YOUR QUERY #3");
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
firestoredb.collection("photos").orderBy("creation", "desc").limit(50)
.get()
.then(function(querySnapshot) {
console.log("YOUR QUERY #4, i.e; Select * from photo LIMIT=50");
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
firestoredb.collection("users")
.where('followedUsers.user2', '==', true)
.get()
.then(function(querySnapshot) {
console.log("Get all the users who follows user2. To use in the Cloud Function");
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
firestoredb.collection("users")
.where('followedPhotosId.9HzWzyXmcnBDuhTZKoQw', '==', true)
.get()
.then(function(querySnapshot) {
console.log("Get all the users who follow photo with Id 9HzWzyXmcnBDuhTZKoQw. To use in the Cloud Function");
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
firestoredb.collection("users").doc("user1").get().then(function(doc) {
if (doc.exists) {
console.log("Update the followedPhotosId object for user1 after a user she/he follows has added a photo with id abcdefghijklmn. To use in the Cloud Function");
var followedPhotosId = doc.data().followedPhotosId;;
Object.assign(followedPhotosId, {abcdefghijklmn: true});
firestoredb.collection("users").doc("user1").set({followedPhotosId: followedPhotosId});
} else {
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
</script>
<body>
</html>
ORDER BY
子句(针对项目1)?UNION
(针对项目2)?JOIN
(针对项目3)?如果没有,那么为什么要远离关系型数据库管理系统? - Rick Jamesorder by
,但不支持joins
和unions
。 - omurbek连接
和嵌套查询
。由于 Firebase 正在不断发展,他们计划支持诸如in
等子句。 - omurbek