貓鼬子場聚集我有一個貓鼬模型稱爲會議與命名當然(課程模型)領域,我想在會議與執行全文搜索全文搜索,還我想使用字段彙總結果從當然子場和選擇像當然,日期等 一些領域我試過如下:全文搜索和項目
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