2016-07-20 3 views
0

그래서 하위 페이지가있는 레코드를 나열하는 페이지 번호가 매겨진 테이블을 구현하고 있으며 필터링 기능을 추가하여 list() 결과를 필터링 할 수 있습니다. 하위 문서 필드 값.mongoose list() - subdocument 필드 값으로 값 필터링

let HardwareSchema = new mongoose.Schema({ 
    name: { type: String, required: true }, // the exact name of the hardware. 
    slug: { type: String, unique: true }, // the hardware's slug. 
    category: { type: String, ref: 'HardwareCategory', required: true }, 
} 

let HardwareCategorySchema = new mongoose.Schema({ 
    name: { type: String, unique: true, required: true }, // the name of the category. 
    slug: { type: String, unique: true }, // the slug of the category. 
    description: { type: String, unique: true, required: true }, // the name of the category. 
    createdBy: { type: String, ref: 'User', required: true } // the original submitter of the document. 
}); 

그래서 기본적으로 선택한 카테고리를 사용하여 Hardwares를 필터링하고 싶습니다.

내 실제 코드는 다음과 같습니다.

const options = { 
    perPage: 10, 
    page: (req.query.page > 0 ? req.query.page : 1) - 1, 
    sortBy: {'rank': -1 }, 
    criteria: { 
     status: 'approved', 
     category: { 
      $elemMatch: { 'slug': category } 
     } 
    } 
}; 

Hardware.list(options) 
.then(results => {}); 

오류가 발생합니다. 문자열에 $ elemMatch를 사용할 수 없습니다.

그래서 기본적으로 필요합니다.

  • 하위 문서 필드 값을 기준으로 결과를 필터링 할 수 있어야합니다.
  • 결과를 페이지 매기기가 가능하도록 결과를 정렬 할 수 있어야합니다.
  • 결과를 정렬 할 수 있어야합니다.

어떻게 관리 할 수 ​​있습니까?

답변

0

이 옵션을 사용해보십시오.

 const options = { 
     perPage: 10, 
     page: (req.query.page > 0 ? req.query.page : 1) - 1, 
     sortBy: {'rank': -1 }, 
     criteria: { 
      status: 'approved', 
      'category.slug' : category 
     } 
    }; 

하거나

 Hardware.find({ 
     'category.slug' : category 

    }, function(err, data){ 
    }) 
같은 발견을()를 사용할 수 있습니다