【发布时间】:2021-06-11 21:33:56
【问题描述】:
我有三个模型User、Profile 和Institution。
用户
const UserSchema = new Schema({
name: {
type: Schema.Types.String,
},
profile: {
type: Schema.Types.ObjectId,
ref: "profiles",
},
});
简介
const ProfileSchema = new Schema({
institution: {
type: Schema.Types.ObjectId,
ref: "institutions",
},
});
机构
const InstitutionSchema = new Schema({
name: {
type: String,
required: true,
},
});
我正在尝试获取用户列表WHERE机构=机构名称:
User.aggregate([
{
$lookup: {
from: "profiles",
let: { profiles_id: "$profile" },
pipeline: [
{
$lookup: {
from: "institutions",
pipeline: [
{
$match: { name: institution_name },
},
],
as: "institution",
},
},
{ $unwind: "$institution" },
],
as: "profile",
},
},
{ $unwind: "$profile" },
{
$project: {
name: "$name",
institution: "$profile.institution.name",
},
},
]);
出于某种奇怪的原因,这是返回所有用户的列表,但它用我使用的过滤器值替换机构字段institution_name。
知道如何解决这个问题吗?
【问题讨论】:
-
你的预期输出是什么?
标签: node.js mongodb mongoose nosql aggregation-framework