【发布时间】:2021-06-12 11:48:37
【问题描述】:
我有这三个模型:
-
Institution模特:
const InstitutionSchema = new Schema({
current_students: [
{
type: Schema.Types.ObjectId,
ref: "users",
},
],
});
module.exports = Institution = mongoose.model("institutions", InstitutionSchema);
在current_students 字段中有对User 模型的引用数组。
-
User模特:
const UserSchema = new Schema({
profile: {
type: Schema.Types.ObjectId,
ref: "profiles",
},
});
module.exports = User = mongoose.model("users", UserSchema);
在profile 字段中引用了Profile 模型。
-
Profile模特:
const ProfileSchema = new Schema({
videoURL: {
type: String,
},
});
module.exports = Profile = mongoose.model("profiles", ProfileSchema);
我正在尝试获取Profile.videoURL 不是null 或undefined 的机构用户的配置文件列表。这是我尝试过的:
Institution.aggregate([
{
$lookup: {
from: "users",
localField: "current_students",
foreignField: "_id",
as: "current_student",
},
},
{
$unwind: {
path: "$current_student",
preserveNullAndEmptyArrays: true,
},
},
{
$lookup: {
from: "profiles",
localField: "current_student.profile",
foreignField: "_id",
as: "current_student_profile",
},
pipeline: [
{
$match: {
videoURL: { $nin: [undefined, null] },
},
},
],
},
]);
但是,由于执行$match 操作的最后一个管道,我不断收到此错误。
Error: Arguments must be aggregate pipeline operators
知道如何解决这个问题吗?
【问题讨论】:
标签: mongodb mongoose aggregation-framework lookup mongoose-populate