2017-10-17 72 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