我一直使用 MySQL 作为数据库,并且看到 NoSQL 的 Parse API 的连接扭曲,我相信我的数据库设计存在缺陷。
这是我使用的:
Game
---------
id
userA
userB
currentRound
RoundScore // A Game can have 0-3 RoundScore
---------
id
game -> Pointer field to Game
user
round
score
(还有一个带有 Parse 的默认 User 集合,并且 Game 和 RoundScore 中所有与用户相关的字段都指向 User 集合。这非常有效)。
看到 Parse API 是如何工作的,我发现很难进行查询:
获取所有游戏
及其(最多 3 个)回合得分
,其中 Game.userA = me
或Game.userB = me
.
我可以轻松获得所有游戏,但没有他们的回合得分
。我无法同时加入两者。
我应该如何处理这个查询或设计? 我应该将 RoundScore 融合到 Game 集合中吗?如果是这样,我应该如何声明新字段?
最佳答案
我已阅读所有这些页面:
- https://parse.com/docs/js_guide#queries
- https://www.parse.com/questions/how-to-achieve-or-condition-in-query
- https://parse.com/questions/combining-queries-or-not-and-modeling-relationships
- https://www.parse.com/docs/js/symbols/Parse.Query.html
我认为以下代码片段应该适合您:
var RoundScoreQuery = new Parse.Query("RoundScore");
var userAQuery = new Parse.Query("Game");
userAQuery.equalTo("userA", "me");
var userBQuery = new Parse.Query("Game");
userBQuery.equalTo("userB", "me");
var gamesQuery = Parse.Query.or(userAQuery, userBQuery);
gamesQuery.find({
success: function(results) {
// results contains all games where "me" is a part of
for (var i = 0; i < results.length; i++) {
var gameId = results[i].get("id");
RoundScoreQuery.equalTo("game", gameId);
query.first({
success: function(object) {
// Successfully retrieved the object with the score
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
},
error: function(error) {
// There was an error.
}
});
关于javascript - 从 SQL 一对多到 Parse NoSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27991615/