wetube 6.20 ~ 6.28

이주왕·2022년 2월 27일
0

Node Js

목록 보기
8/10

Video.findById()

Video.findById(id)를 통해 해당 id의 video를 찾을 수 있다.
Video.findOne()은 () 안에 filter을 통해서 video를 찾을 수 있다.

Join()

join() method는 array의 모든 요소들은 연결해 하나의 문자열로 만들어 준다.

ex) ["a","b","c"].join() -- "a","b","c"

Arrow Function

hashtags.map((word) => (word.startswith("#") ? word : `#${word}))

? = if를 의미
: = or를 의미

Video.findByIdAndUpdate()

Video.findByIdAndUpdate()를 통해 video를 edit 하여 update 할 수 있다.
Video.findByIdAndUpdate()의 첫번째 argument에는 video의 id를, 두번째 argument에는 업데이트 할 내용을 넣어야한다.

Video.exists()

video.exists()를 통해 해당 video가 있는지 없는지 true, false 형태로 반환해준다.
() 안에는 filter을 넣어서 원하는 검색 조건을 지정할 수 있다.

ex) video.exists({_id:id})

Mongoose Middleware

middleware를 생성하려면 반드시 model이 생성되기 이전에 코드를 작성해야 한다.

videoSchema.pre("save", async function() {
	console.log(this)
    })

this는 해당 video의 정보를 json 형태로 반환한다.

Static

videoSchema.static()을 통해서 새로운 static function을 만들 수 있다.

videoSchema.static("formatHashtags", function (hashtags) {
    return hashtags.split(",").map((hashtags) => (hashtags.startsWith("#") ? hashtags : `#${hashtags}`))
})

Video.findByIdAndDelete()

Video.findByIdAndDelete(id)를 통해서 video를 삭제할 수 있다.
대부분의 경우에는 remove 대신 delete를 사용한다.

Sort()

sort()를 통해서 video를 정렬할 수 있다.

video.sort({createdAt:"desc"})

Req.Query

req.query로 url의 name value를 가져올 수 있다.

let을 통한 update

let videos
if (keyword) {
	videos = await Video.find({})
    }

let videos를 한 후 const 나 let을 사용하지 않고 videos를 instantiate를 하면 let videos의 빈 array에 update가 된다.

Regex

$regex를 통해서 regular expression을 보낼 수 있다.

videos = await Video.find({
            title: {
                $regex: new RegExp(keyword, "i")
            }
        })

(Mongo DB Regex)
링크텍스트

(RegExp Mdn)
링크텍스트

profile
Learn more

0개의 댓글

관련 채용 정보