MongoDb:管道内部查找错误:错误:参数必须是聚合管道运算符



我有这三种型号:

  1. Institution模型:
const InstitutionSchema = new Schema({
current_students: [
{
type: Schema.Types.ObjectId,
ref: "users",
},
],
});
module.exports = Institution = mongoose.model("institutions", InstitutionSchema);

在CCD_ 3领域中对CCD_。

  1. User型号:
const UserSchema = new Schema({
profile: {
type: Schema.Types.ObjectId,
ref: "profiles",
},
});
module.exports = User = mongoose.model("users", UserSchema);

对CCD_ 6领域的CCD_。

  1. Profile型号:
const ProfileSchema = new Schema({
videoURL: {
type: String,
},
});
module.exports = Profile = mongoose.model("profiles", ProfileSchema);

我正在尝试获取Profile.videoURL不是nullundefined的机构的用户的配置文件列表。这就是我尝试过的:

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

知道怎么解决这个问题吗?

您的查询是错误的。你不能那样通过pipeline。看看$lookup语法。此外,如果你想了解更多关于聚合的信息,我建议你参加MongoDB自己提供的聚合课程。它是免费的。

尝试此查询:

db.institutions.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",
}
},
{ $unwind: "$current_student_profile" },
{
$match: {
"current_student_profile.videoURL": { $nin: [undefined, null] },
}
}
]);

输出:

/* 1 createdAt:3/13/2021, 6:18:26 PM*/
{
"_id" : ObjectId("604cb49a6b2dcb17e8b152b2"),
"name" : "Institute 1",
"current_students" : [
ObjectId("604cb4c36b2dcb17e8b152b8"),
ObjectId("604cb4c36b2dcb17e8b152b9")
],
"current_student" : {
"_id" : ObjectId("604cb4c36b2dcb17e8b152b8"),
"name" : "Dheemanth Bhat",
"profile" : ObjectId("604cb4b16b2dcb17e8b152b5")
},
"current_student_profile" : {
"_id" : ObjectId("604cb4b16b2dcb17e8b152b5"),
"videoURL" : "http://abc1@xyz.com"
}
},
/* 2 createdAt:3/13/2021, 6:18:26 PM*/
{
"_id" : ObjectId("604cb49a6b2dcb17e8b152b3"),
"name" : "Institute 2",
"current_students" : [
ObjectId("604cb4c36b2dcb17e8b152ba")
],
"current_student" : {
"_id" : ObjectId("604cb4c36b2dcb17e8b152ba"),
"name" : "Alex Rider",
"profile" : ObjectId("604cb4b16b2dcb17e8b152b7")
},
"current_student_profile" : {
"_id" : ObjectId("604cb4b16b2dcb17e8b152b7"),
"videoURL" : "http://abc3@xyz.com"
}
}

测试数据:

users集合:

/* 1 createdAt:3/13/2021, 6:19:07 PM*/
{
"_id" : ObjectId("604cb4c36b2dcb17e8b152b8"),
"name" : "Dheemanth Bhat",
"profile" : ObjectId("604cb4b16b2dcb17e8b152b5")
},
/* 2 createdAt:3/13/2021, 6:19:07 PM*/
{
"_id" : ObjectId("604cb4c36b2dcb17e8b152b9"),
"name" : "Ahmed Ghrib",
"profile" : ObjectId("604cb4b16b2dcb17e8b152b6")
},
/* 3 createdAt:3/13/2021, 6:19:07 PM*/
{
"_id" : ObjectId("604cb4c36b2dcb17e8b152ba"),
"name" : "Alex Rider",
"profile" : ObjectId("604cb4b16b2dcb17e8b152b7")
}

profiles集合:

/* 1 createdAt:3/13/2021, 6:18:49 PM*/
{
"_id" : ObjectId("604cb4b16b2dcb17e8b152b5"),
"videoURL" : "http://abc1@xyz.com"
},
/* 2 createdAt:3/13/2021, 6:18:49 PM*/
{
"_id" : ObjectId("604cb4b16b2dcb17e8b152b6")
},
/* 3 createdAt:3/13/2021, 6:18:49 PM*/
{
"_id" : ObjectId("604cb4b16b2dcb17e8b152b7"),
"videoURL" : "http://abc3@xyz.com"
}

institutions集合

/* 1 createdAt:3/13/2021, 6:18:26 PM*/
{
"_id" : ObjectId("604cb49a6b2dcb17e8b152b2"),
"name" : "Institute 1",
"current_students" : [
ObjectId("604cb4c36b2dcb17e8b152b8"),
ObjectId("604cb4c36b2dcb17e8b152b9")
]
},
/* 2 createdAt:3/13/2021, 6:18:26 PM*/
{
"_id" : ObjectId("604cb49a6b2dcb17e8b152b3"),
"name" : "Institute 2",
"current_students" : [
ObjectId("604cb4c36b2dcb17e8b152ba")
]
}

必须是这个:

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",
}
},
{
$match: {
videoURL: { $nin: [undefined, null] },
},
}
]);

注意,这看起来像一个";"一对一";从关系型RDBMS设计的转换。通常,将每个转换为集合不是最佳方法,通常集合应该有不同的设计。

最新更新