Mongoose子字段聚合与全文搜索和项目

时间:2017-10-17 17:10:47

标签: node.js mongodb mongoose aggregation-framework

我有一个名为会话 Mongoose 模型,其中包含一个名为课程课程模型)的字段,我想要要使用全文搜索对会话执行全文搜索,我还想使用课程子字段中的字段汇总结果,并选择一些字段,例如课程日期等 我尝试了以下方法:

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 } );

我不确定我尝试的是否会给我带来我想要的东西,而且我收到有关 $ unwind 运算符的错误:

  

MongoError:exception:$ unwind字段路径'$ course'结束时的值   必须是一个数组,但是是一个OID

任何形式的帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

您可以尝试以下聚合。

通过将课程文档中的课程对象ID加入课程文档中的id,您缺少提取课程文档所需的In [12]: query = db.session.query().\ ...: select_from(Post).\ ...: outerjoin(creator, Post.creator).\ ...: outerjoin(last_modifier, Post.last_modifier)

$lookup阶段在输出中保留所需的字段。

$project

课程是一个包含一个课程文档的数组。您可以使用Session.aggregate([ { "$match": { "$text": { "$search": "web" } } }, { "$lookup": { "from": "courses", "localField": "course", "foreignField": "_id", "as": "course" } }, { "$project": { "course": 1, "date": 1, "address": 1, "available": 1 } } ]) 投影文档。

$arrayElemAt