모든 nodejs/mongoose 개발자가 알아야 할 기본 작업
17001 단어 nodebeginnerstypescriptmongodb
문서 삽입
const cap = {
color: Yellow,
size: 'G',
brand: Nike
}
const capDb = await Cap.create(cap)
// capDb = { _id: objectId(''), color: Yellow,size: G, brand:Nike }
문서 제거
const cap = {
color: Yellow,
size: 'G',
brand: Nike,
_id: '123'
}
const capRemoved = await Cap.findByIdAndDelete(cap._id)
// capRemoved = { _id: objectId(''), color: Yellow,size: G, brand:Nike }
특정 문서 찾기
const cap = {
color: Yellow,
size: 'G',
brand: Nike,
_id: '123'
}
const capDb = await Cap.findById(cap._id)
// capDb = { _id: objectId(''), color: Yellow,size: G, brand:Nike }
// or
const capDb = await Cap.findOne({color: Yellow, size: 'G'})
// capDb = { _id: objectId(''), color: Yellow,size: G, brand:Nike }
// note: findOne will return the first document that matches the filter specified if an _id is not provided
문서 필터링
// find only Yellow caps from the 'Nike' brand and model 'Special'
interface cap {
brand: string
color: string
size: string
model: string
}
const caps = await Cap.find({color: 'Yellow' brand: 'Nike', model: 'Special'})
배열 필드와 일치하는 문서 필터링
// find the games which has 'PS5' platform
interface game {
genre: string
title: string
platforms: string[]
}
const gamesDb = [
{_id: '1', genre: adventure, title: 'God of War', platforms: ['PS3', 'PS4', 'PS5']},
{_id: '2', genre: adventure, title: 'Demon souls remake', platforms: ['PS5']},
{_id: '2', genre: adventure, title: 'GTA IV', platforms: ['PS3']},
{_id: '2', genre: adventure, title: 'Forza horizon', platforms: ['XBOX 360', 'XBOX ONE']}
]
const games = await Game.find({platforms: 'PS5'})
/* games = [
{_id: '1', genre: adventure, title: 'God of War', platforms: ['PS3', 'PS4', 'PS5']},
{_id: '2', genre: adventure, title: 'Demon souls remake', platforms: ['PS5']},
]
*/
배열 필드와 일치하는 문서 필터링
// find the games which has 'PS5' and 'PS3' platform
interface game {
genre: string
title: string
platforms: string[]
}
const gamesDb = [
{_id: '1', genre: adventure, title: 'God of War', platforms: ['PS3', 'PS4', 'PS5']},
{_id: '2', genre: adventure, title: 'Demon souls remake', platforms: ['PS5']},
{_id: '2', genre: adventure, title: 'GTA IV', platforms: ['PS3']},
{_id: '2', genre: adventure, title: 'Forza horizon', platforms: ['XBOX 360', 'XBOX ONE']}
]
const games = await Game.find({platforms: {$in: ['PS3','PS5']})
/*games = [
{_id: '1', genre: adventure, title: 'God of War', platforms: ['PS3', 'PS4', 'PS5']},
{_id: '2', genre: adventure, title: 'GTA IV', platforms: ['PS3']}
]
*/
문서의 객체 필드 배열에서 객체 소품 업데이트
// update a document where the monster arm size is 700 and set to 30 and tattoo to true
interface monster {
arms: [{size: number, tattoo: boolean, side: string}]
}
const monster = {
_id: '1',
arm: {
size: 700,
tattoo: false,
side: 'left'
}
const monsterUpdated = await Monster.findOneAndUpdate(
{'arms.side': 'left'},
{'arms.$.size': 30, 'arms.$.tattoo': true},
{new: true}
)
/* monsterUpdated = {
_id: '1',
arms: [{
size: 30,
tattoo: true,
side: 'left'
}]
*/
Reference
이 문제에 관하여(모든 nodejs/mongoose 개발자가 알아야 할 기본 작업), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/felipeleao18/operations-that-every-nodejsmongoose-developer-should-know-31f4텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)