猫鼬子场聚集我有一个猫鼬模型称为会议与命名当然(课程模型)领域,我想在会议与执行全文搜索全文搜索,还我想使用字段汇总结果从当然子场和选择像当然,日期等 一些领域我试过如下:全文搜索和项目
Session.aggregate(
[
{
$match: { $text: { $search: 'web' } }
},
{ $unwind: '$course' },
{
$project: {
course: '$course',
date: '$date',
address: '$address',
available: '$available'
}
},
{
$group: {
_id: { title: '$course.title', category: '$course.courseCategory', language: '$course.language' }
}
}
],
function(err, result) {
if (err) {
console.error(err);
} else {
Session.deepPopulate(result, 'course course.trainer
course.courseCategory', function(err, sessions) {
res.json(sessions);
});
}
}
);
我的模型:
会议
schema = new mongoose.Schema( { date: { type: Date, required: true }, course: { type: mongoose.Schema.Types.ObjectId, ref: 'course', required: true }, palnning: { type: [Schedule] }, attachments: { type: [Attachment] }, topics: { type: [Topic] }, trainer: { type: mongoose.Schema.Types.ObjectId, ref: 'trainer' }, trainingCompany: { type: mongoose.Schema.Types.ObjectId, ref: 'training-company' }, address: { type: Address }, quizzes: { type: [mongoose.Schema.Types.ObjectId], ref: 'quiz' }, path: { type: String }, limitPlaces: { type: Number }, status: { type: String }, available: { type: Boolean, default: true }, createdAt: { type: Date, default: new Date() }, updatedAt: { type: Date } }, { versionKey: false } );
- 场
let schema = new mongoose.Schema( { title: { type: String, required: true }, description: { type: String }, shortDescription: { type: String }, duration: { type: Duration }, slug: { type: String }, slugs: { type: [String] }, program: { content: { type: String }, file: { type: String } }, audience: [String], requirements: [String], language: { type: String, enum: languages }, price: { type: Number }, sections: [Section], attachments: { type: [Attachment] }, tags: [String], courseCategory: { type: mongoose.Schema.Types.ObjectId, ref: 'course-category', required: true }, trainer: { type: mongoose.Schema.Types.ObjectId, ref: 'trainer' }, trainingCompany: { type: mongoose.Schema.Types.ObjectId, ref: 'training-company' }, status: { type: String, default: 'draft', enum: courseStatus }, path: { type: String }, cover: { type: String, required: true }, duration: { type: Number, min: 1 }, createdAt: { type: Date, default: Date.now }, updatedAt: { type: Date } }, { versionKey: false } );
我不知道,如果是我的尝试是要带我我想要什么,我得到关于$此错误放松操作:
MongoError: exception: Value at end of $unwind field path '$course' must be an Array, but is a OID
任何形式的帮助将是非常赞赏。
你缺少在课程文档中从会议文件加入了关于课程对象ID来标识拉过程文档所需'$ lookup'。在'$ match'和'$ unwind'阶段之间插入查找阶段。像'{:从{ : $查找 “当然”, localField: “当然”, foreignField: “_id”, 为: “当然” } }'。调整为使用正确的收藏名称,本地字段和外地字段。 – Veeram
@Veeram我得到这个错误:'MongoError:异常:无法识别的管道艺名:“$ lookup'' – jemlifathi
我的猫鼬版**^4.11.12 **如果它做的感觉。 – jemlifathi