$lookup 在对象数组中查找 id [英] $lookup for id inside array of objects
问题描述
我有一些看起来像这样的模型:
const ExerciseDetailSchema = new mongoose.Schema({翻译:{名称: {en: { type: String, required: true },否:{ 类型:字符串}}}});const trainingDetailSchema = new mongoose.Schema({套: {练习:[锻炼练习],单位:{ 类型:字符串}}});const WorkoutSets = new mongoose.Schema({代表:{ type: Number },重量:{ 类型:数量}});const WorkoutExercises = new mongoose.Schema({练习 ID:{ 类型:字符串,必填:真},组数:[锻炼组]});
基本上,锻炼由集合组成,其中包含一些元数据,包括练习.这些练习是一个由练习集组成的数组.
我正在尝试进行查询,该查询将返回包括锻炼名称在内的锻炼详细信息,使其看起来像这样:
<代码>{_id:5f60dc1069c27c015ede4e3e",套: {单位:'公制',练习: [{_id:5f60dc1069c27c015ede4e3e",练习 ID:5f60c3b7f93d8e00a1cdf414",练习名称:{zh: "深蹲",否:Knebøy"},套: [{_id:5f60dc1069c27c015ede4e3f",代表:10},{_id:5f60dc1069c27c015ede4e40",代表:20}]}]}}
这将是一个示例练习,例如:
<代码>{_id:5f60c3b7f93d8e00a1cdf414",翻译:{名称: {zh: "深蹲",否:Knebøy"}}}
我尝试过使用 $lookup,如下所示:
const training = await WorkoutDetail.aggregate([{$查找:{来自:ExportDetail.collection.name,localField: "Sets.Exercises.ExerciseId",外国字段:_id",如:练习细节"}}]);
但它总是只返回:ExerciseDetail: []
对于应该加入"的部分.任何人都可以提供有关如何正确查询的任何帮助吗?
更新
我在这里的另一个问题中提出了关于对这些数据进行分组的问题,并收到了一个完美的答案.希望它也可以对其他人有所帮助:MongoDB $group(mongo 游乐场)
由于 Exercises
是一个数组,你需要 $unwind
来扁平化数组.><预><代码>[{$unwind: "$Sets.Exercises";},{$查找:{来自:exerciseDetailSchema",localField: "Sets.Exercises.ExerciseId",外国字段:_id",如:练习细节"}}]
注意:要重组你需要使用$group
.由于 lookup
工作 Mongo 游乐场
I have some models that look like this:
const exerciseDetailSchema = new mongoose.Schema(
{
Translations: {
Name: {
en: { type: String, required: true },
no: { type: String }
}
}
}
);
const workoutDetailSchema = new mongoose.Schema(
{
Sets: {
Exercises: [ WorkoutExercises ],
Units: { type: String }
}
}
);
const WorkoutSets = new mongoose.Schema({
Reps: { type: Number },
Weight: { type: Number }
});
const WorkoutExercises = new mongoose.Schema({
ExerciseId: { type: String, required: true },
Sets: [ WorkoutSets ]
});
Basically, workouts are made of Sets, which contain some metadata including exercises. These exercises is an array which is made of workout sets.
I am trying to make a query that will return to me the Workout Details including the Exercise Name, so that it can look like this:
{
_id: "5f60dc1069c27c015ede4e3e",
Sets: {
Units: 'metric',
Exercises: [
{
_id: "5f60dc1069c27c015ede4e3e",
ExerciseId: "5f60c3b7f93d8e00a1cdf414",
ExerciseName: {
en: "Squat",
no: "Knebøy"
},
Sets: [
{ _id: "5f60dc1069c27c015ede4e3f", Reps: 10 },
{ _id: "5f60dc1069c27c015ede4e40", Reps: 20 }
]
}
]
}
}
This would be with an example exercise like:
{
_id: "5f60c3b7f93d8e00a1cdf414",
Translations: {
Name: {
en: "Squat",
no: "Knebøy"
}
}
}
I have tried using a $lookup, like this:
const workout = await WorkoutDetail.aggregate([
{
$lookup: {
from: ExerciseDetail.collection.name,
localField: "Sets.Exercises.ExerciseId",
foreignField: "_id",
as: "ExerciseDetail"
}
}
]);
But it is always returning with just: ExerciseDetail: []
for the part that is supposed to be "joined". Can anyone provide any help with how to query this properly?
UPDATE
I have asked in another question here about grouping this data, and received an answer that had done it perfectly. Hopefully it can be helpful to others also: MongoDB $group (mongo playground)
Since the Exercises
is an array, you need to $unwind
to flat the array.
[
{
$unwind: "$Sets.Exercises"
},
{
$lookup: {
from: "exerciseDetailSchema",
localField: "Sets.Exercises.ExerciseId",
foreignField: "_id",
as: "ExerciseDetail"
}
}
]
Note : To restructure you need to use $group
. I've not shown since you had a problem with lookup
Working Mongo playground
这篇关于$lookup 在对象数组中查找 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!