[英]How to join data from two collections in MongoDB?
I'am developing some application on MEAN.js boilerplate and I've got one problem which I'm not able to solve by myself :( I've got a following database scheme: 我正在MEAN.js样板上开发一些应用程序,但遇到了一个我自己无法解决的问题:(我有以下数据库方案:
var UserSchema = new Schema({
profession: {
type: Schema.ObjectId,
ref: 'Profession'
},
game: {
type: Schema.ObjectId,
ref: 'Game'
}
};
var ProfessionSchema = new Schema({
assignedTaskCategories: [{
type: Schema.ObjectId,
ref: 'TaskCategory'
}]
});
var TaskCategorySchema = new Schema({
professions: [{
type: Schema.ObjectId,
ref: 'Profession'
}],
assignedToGame: {
type: Schema.ObjectId,
ref: 'Game'
}
});
var TaskSchema = new Schema({
game: {
type: Schema.ObjectId,
ref: 'Game'
},
inCategories: [{
type: Schema.ObjectId,
ref: 'TaskCategory'
}]
});
Now I would like to get all Tasks, which have got some element in inCategories
same as User's profession taskCategories array. 现在,我想获取所有任务,这些任务在
inCategories
与用户的行业taskCategories数组相同。 I've tried this, but it returns nothing 我已经试过了,但是什么也没返回
Profession.find({ _id: req.user.profession }).exec(function(err, userCategories) {
if(err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
Task.find({ game: req.user.game, inCategories: userCategories.assignedTaskCategories}).exec(function(err, tasks) {
if(err) {
return res.status(400).sed({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(tasks);
}
});
}
});
Could anyone help me with this? 有人可以帮我吗? :) If my approach is bad, please tell me the right way how can I solve it :)
:)如果我的方法不好,请告诉我正确的方法如何解决它:)
We cannot use $setEquals, because the array are not same, consider following example: 我们不能使用$ setEquals,因为数组不一样,请考虑以下示例:
Profession programmer is able to solve following task categories: Programming, Presentation, Wash dishes 专业的编程人员能够解决以下任务类别: 编程,演示,洗碗
Profession secretary is able to solve following task categories: Presentation, Wash dishes 专业秘书能够解决以下任务类别: 演示,洗碗
Then we will create a task, which is assigned to category: Wash dishes , so the professions assignedCategories
arrays are bigger, not equal to task's inCategories
array. 然后,我们将创建一个任务,分配给类别: 洗碗 ,这样的职业
assignedCategories
阵列大,不等于任务的inCategories
阵列。
Try using the aggregation framework where you employ the $setEquals
operator to compare the arrays. 尝试在使用
$setEquals
运算符比较数组的地方使用聚合框架 。 In the following pipeline, the operator determines if the Tasks' inCategories
array and the assignedTaskCategories
array contain the same elements: 在下面的管道时,操作者确定是否任务
inCategories
阵列和assignedTaskCategories
阵列包含相同的元素:
Profession.find({ _id: req.user.profession }).exec(function(err, userCategories) {
if(err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
var pipeline = [
{
"$match": { "game": req.user.game }
},
{
"$project": {
"game": 1,
"inCategories": 1,
"sameElements": {
"$setEquals": [ "$inCategories", userCategories[0].assignedTaskCategories ]
}
}
},
{
"$match": { "sameElements": true }
}
];
Task.aggregate(pipeline)
.exec(function (err, tasks){
if(err) {
return res.status(400).sed({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(tasks);
}
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.