티스토리 뷰
Query Helper는 instance method와 유사하지만 mongoose queries에만 쓰이는 helper function이다.
Query Helper는 말 그대로 mongoose의 chainable 한 mongoose의 query builder API를 확장시켜주는 method다.
스키마를 정의.
const animalSchema = new Schema({
name: String,
breed: String
});
query property에 query helper를 정의후 컴파일.
animalSchema.query.byName = function (name) {
return this.where({ name: new RegExp(name, 'i') });
}
const Animal = mongoose.model('Animal', animalSchema);
chainable 한 query 실행.
Animal.find().byName('fido').exec(function (err, animals) {
console.log(animals);
});
// [
// {
// _id: 5e65e6acf572ba594ced0828,
// name: 'fido',
// breed: 'Jindo',
// __v: 0
// }
// ]
공식 레퍼런스: https://mongoosejs.com/docs/guide.html
git repository: https://github.com/taeheongo/MongoDB
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 자료구조
- map
- resave
- javascript
- pm2 #cluster #Javascript
- mixin
- 이중 연결리스트
- express-session
- alias
- 집합
- instance method
- mongoose
- saveUnitialized
- 맵
- set
- Iterator
- Generator
- 다형성
- 선형리스트
- query helper
- static method
- 다중상속
- Node.js
- index
- virtuals
- MongoDB
- 자료구조 #딕셔너리 #해시
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함