【发布时间】:2017-03-13 18:21:11
【问题描述】:
我正在尝试在我一直在处理的 API 中放置一个嵌套的 MongoDB 调用。 API 的目标是根据运动表现更新个人目标。我目前遇到的问题是尝试将运动事件放入主函数中,以使我能够计算“完整性”。
我欢迎您对这个问题提出任何意见。
大家好,
我正在尝试在我一直在处理的 API 中放置一个嵌套的 MongoDB 调用。API 的目标是根据运动表现更新个人目标。我目前遇到的问题是试图将运动事件放入主要的 使我能够计算“完整性”的函数。
我欢迎您对这个问题提出任何意见。
module.exports.generateGoal = function(request, response) {
//User ID
var user_id = new ObjectId(request.params.id);
//Goal Search
Goals.find({
user_id: user_id,
active_goal: true
}, function(err, goals) {
if (err) {
//Error: send error to user
throw err
} else {
if (goals.length == 0) { //No goals, do nothing
return response.json({
success: true,
msg: "No Goal"
});
} else { //User as goals, therefore pull in exercise
for (i = 0; i < goals.length; i++) {
//Looking to have exercise available here for computation
queryExercise(user_id, goals[i].start_date, goals[i].end_date, function(result) {
//Exercise able to be accessed here
console.log(result)
return result
});
}
}
}
});
};
function queryExercise(user_id, start_date, end_date, callback) {
ExerciseData.find({
user_id: user_id,
exercise_date: {
$gte: start_date,
$lt: end_date
}
}, function(err, result) {
if (err) {
console.log(err);
} else if (result.length > 0) {
callback(result);
}
});
}
编辑 2:
我非常感谢下面的回复,这正是我所需要的。展望未来,任何目标都可以进行多次练习,获得类似于以下输出的最佳方法是:
{
_id: dfhdjds8348hhj8
goal_id: 1
exercises: { {
exercise: 1,
type: running
},
{
exercise: 2,
type: running
}
}
}.
{
_id: ddhdjds8342hhj8
goal_id: 2
exercises: { {
exercise: 1,
type: jumping
},
{
exercise: 2,
type: rowing
}
}
}
编辑 3:
在执行下面的新代码后,我注意到它无法返回正确的值。我想提供一些关于我使用的模态和语法的更多信息。
目标
var mongoose = require('mongoose');
// Define goal model schema
var GoalsSchema = new mongoose.Schema({
user_id: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
active_goal: Boolean,
goal_id: Number,
start_date: Date,
end_date: Date
}, {
timestamps: true
});
// Export user model
module.exports = mongoose.model('Goals', GoalsSchema);
运动数据
var mongoose = require('mongoose');
// Define user model schema
var exercisesSchema = new mongoose.Schema({
user_id: {
type: mongoose.Schema.Types.ObjectId,
required: true
},
exercise_date: Date,
exercise_type: String
}, {
timestamps: true
});
// Export user model
module.exports = mongoose.model('exercises', exercisesSchema);
我在代码中使用的语法如下,而不是我必须进行的其他更改才能使其正常工作。
module.exports.generateGoal = function(request, response) {
//User ID
var user_id = new ObjectId(request.params.id); //User ID
//Goal query
Goals.aggregate([{
"$match": {
"user_id": user_id,
"active_goal": true
}
},
{
"$lookup": {
"from": "exercises",
"localField": "user_id",
"foreignField": "user_id",
"as": "exercises"
}
},
{
"$unwind": {
"path": "$exercises",
"preserveNullAndEmptyArrays": true
}
},
{
"$redact": {
"$cond": [{
"$and": [{
"$gte": ["$exercises.exercise_date", "$exercises.start_date"]
},
{
"$lte": ["$exercises.exercise_date", "$exercises.end_date"]
}
]
},
"$$KEEP",
"$$PRUNE"
]
}
},
{
"$group": {
"_id": "$_id",
"goal_id": {
"$first": "$goal_id"
},
"exercises": {
"$push": "$exercises"
}
}
}
],
function(err, goalOut) {
console.log(goalOut)
if (err) {
//Error: send error to user
throw err
} else {
if (goalOut.length == 0) { //No goals, do nothing
return response.json({
success: true,
msg: "No Goal",
statusCode: 0,
payload: 'na'
});
} else { //User as goals + matching exercise
//console.log(goals);
return response.json({
success: true,
msg: "Goals",
payload: goalOut
});
}
}
});
};
输出如下:
[ { _id: 58c3e0b1c8a467055d900595, goal_id: 1, exercises: [] },
{ _id: 58c3e0adc8a467055d900594, goal_id: 2, exercises: [] } ]
正如您所见,练习数据实际上是一个空数组,尽管在原始帖子中存在以下数据。
已解决
我忘了注意收藏名称的脉动
【问题讨论】:
-
主函数中需要queryExercise的结果??现在执行上述代码的错误是什么??
-
我相信问题可能在这里
"$exercises.drink_date";您的Exercises架构中没有drink_date。 -
在我打字时很尴尬,出于某种原因,我正在考虑喝一杯。刚刚检查了我的源代码,它应该是goal_date。我已经修改了帖子。
-
我找到了解决方案。 MongoDB 正在使用该集合的复数版本。纠正它就像一个魅力。非常感谢您的帮助。
标签: javascript node.js mongodb mongoose mongodb-query