有人可以解释一下在像 parse.com 这样的 noSQL 环境中正确的数据结构是什么,以执行 SQL 中的 LEFT JOIN 等价操作。我来自关系世界,所以我非常愿意接受我的表格需要完全重新设计的想法。
我想存储“职位”和“用户”,用户可以在其中发布职位和申请职位。所以我创建了三个表:“Job”、“User”和“Application”。应用程序有三列:“指向作业的指针”、“指向用户的指针”和“应用程序状态”。
问题是:
我无法在单个查询中获取用户发布的所有职位以及每个职位的申请数量。
我无法在单个查询中获取用户的所有可用工作,也无法知道该用户是否申请了
我正在为这个项目使用 parse.com。
编辑赏金
我有多个解决方案,可以使用多个查询或在作业表中使用数组。每个都有局限性,但我希望了解是否有更好的数据结构或查询来获取所有工作以及这些工作的应用程序。
最佳答案
这是可行的。如果您允许少量查询而不是一个查询,有些事情会更容易实现。但我认为您的特定问题可以通过一个查询来完成,前提是您允许对结果进行一些内存操作。
// 1. assume that Job contains a pointer to the user who posted it (postingUser)
// 2. assume that Application's user column means the user who applied
// 3. Assume Javascript, though the sdk contains functional equivalents for several languages
var user = // a given user
var jobQuery = new Parse.Query("Job");
jobQuery.equalTo("postingUser", user);
// if you wanted all the jobs posted by user, we'd be ready to go
// but you also want application counts, which forces us to be more roundabout...
var appQuery = new Parse.Query("Application");
appQuery.matchesQuery("job", jobQuery); // this is the nearest idea to LEFT JOIN
appQuery.include("job"); // eagerly fetch the related job
appQuery.find().then(function(applications) {
// applications are applications for jobs posted by user
// so here's the little in-memory work to get jobs (using underscorejs)
var jobs = [];
var appCounts = {};
_.each(applications, function(app) {
var job = app.get("job"); // remember job was eagerly fetched
jobs.push(job);
appCounts[job] = (appCounts[job])? appCounts[job]+1 : 1;
// Taking your second question to mean "is the user who
// posted the job also one of its applicants?"
job["posterIsApplicant"] = app.get("user").id == user.id;
});
});
当上述完成后,jobs
将包含 user
发布的职位,appCounts[job]
将是任何应用程序的数量job 和 job["posterIsApplicant"]
仅当该职位的申请人之一也是发布该职位的用户时才为真。
关于javascript - Parse.com LEFT JOIN 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29716664/