2017-10-17 96 views
0

猫鼬子场聚集我有一个猫鼬模型称为会议与命名当然课程模型)领域,我想在会议与执行全文搜索全文搜索,还我想使用字段汇总结果从当然子场和选择像当然日期等 一些领域我试过如下:全文搜索和项目

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

任何形式的帮助将是非常赞赏。

+0

你缺少在课程文档中从会议文件加入了关于课程对象ID来标识拉过程文档所需'$ lookup'。在'$ match'和'$ unwind'阶段之间插入查找阶段。像'{:从{ : $查找 “当然”, localField: “当然”, foreignField: “_id”, 为: “当然” } }'。调整为使用正确的收藏名称,本地字段和外地字段。 – Veeram

+0

@Veeram我得到这个错误:'MongoError:异常:无法识别的管道艺名:“$ lookup'' – jemlifathi

+0

我的猫鼬版**^4.11.12 **如果它做的感觉。 – jemlifathi

回答

1

你可以试试下面聚集。

你缺少$lookup在课程文档中从会议文件加入了关于课程对象ID来标识拉过程文档所需。

$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来投影文档。

"course": {"$arrayElemAt":["$course", 0]} 
+0

它增加了** $比赛时工作正常**除外即使例如像一个真正的条件: '{ \t \t \t \t $比赛:{“可用”:真正} }' – jemlifathi

+0

你的意思是当你添加“可用”来匹配舞台时不工作?类似于“{ ”$ match“:{ ”$ text“:{ ”$ search“:”web“ },”available“:true } }'应该有效。 – Veeram

+0

不,甚至只有'{“$ match”:{“available”:true}}'不起作用 – jemlifathi