티스토리 뷰
Search
몽고DB에서 데이터를 검색하는 역할입니다

https://mongoosejs.com/docs/api.html#aggregate_Aggregate-search
Mongoose v5.12.13: API docs
mongoosejs.com
1. Search PUG 페이지 만들기
method 는 GET 을 사용하고 input name=keyword 값을 주어서 url에서도 무엇을 검색했는지 알 게 만듭니다


2. search 페이지로 가는 라우터를 추가합니다

3. search에 사용되는 컨트롤러를 만듭니다
첫번째로 console.log(req.query)를 사용하여 로그창에 작동되는지 체크합니다


제대로 나온 것을 확인 한 후

keyword 함수값을 정해주고 let videos 배열을 생성한 후 if 문 안에서 videos를 업데이트하는 형식입니다
if 문 밖에 let videos 한 이유는 if 문 안에서 const videos를 하였을 때 검색하여 찾으면
return res.render("search",{ pageTitle : "Search" , videos }); 이 정상 작동 되지만
검색한 데이터가 존재하지 않는다면
return res.render("search",{ pageTitle : "Search" , videos }); 는 videos 가 undified이기 때문에 오류가 날 것이기에
let videos를 하였습니다
또한 $regex는 몽고DB에 있는 기능 중 하나이고 $,^ 를 사용하여 맨 처음 맨 끝에 있는단어 검색을 지정할 수도 있습니다.
그리고 자주 사용되는 몽고DB의 함수명 중 하나는 $gte 입니다 예를들어 비디오의 뷰 50이상인 것을 검색할때 등 숫자를 이용한 검색을 설정할 때 사용됩니다
https://docs.mongodb.com/manual/reference/operator/query/regex/
$regex — MongoDB Manual
Reference > Operators > Query and Projection Operators > Evaluation Query Operators $regexProvides regular expression capabilities for pattern matching strings in queries. MongoDB uses Perl compatible regular expressions (i.e. "PCRE" ) version 8.42 with UT
docs.mongodb.com
https://docs.mongodb.com/manual/reference/operator/query/gte/
$gte — MongoDB Manual
Reference > Operators > Query and Projection Operators > Comparison Query Operators $gteSyntax: {field: {$gte: value} }$gte selects the documents where the value of the field is greater than or equal to (i.e. >=) a specified value (e.g. value.)For most dat
docs.mongodb.com

- Total
- Today
- Yesterday
- 윤성우 열혈C프로그래밍
- Storybook
- NextApiRequest
- WSL2
- 프리온보딩
- 아차산
- 스토리 북
- NextRequest
- error
- 노개북
- electron
- nextjs
- 프론트앤드
- 위코드
- 초보
- env
- 북클럽
- 원티드
- TopLayer
- CLASS
- nodejs
- 우아한테크코스
- C언어
- import/order
- React
- createPortal
- 노마드코더
- javascript
- jest
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |