Database/monogodb

[MongoDB] ํ”„๋กœ์ ์…˜ (Projection) ์ด๋ž€? projection query ์‚ฌ์šฉ๋ฒ•

yuri lee 2022. 12. 20. 01:32
๋ฐ˜์‘ํ˜•

Intro

MongoDB์˜ ํ”„๋กœ์ ์…˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

What is projection?

MongoDB์—์„œ projection(ํ”„๋กœ์ ์…˜)์€ ํ•œ document ์ „์ฒด๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํŠน์ • ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด document๊ฐ€ 3๊ฐœ์˜ ํ•„๋“œ๋ฅผ ๊ฐ–๊ณ  ์žˆ์„ ๋•Œ, 1๊ฐœ์˜ ํ•„๋“œ๋งŒ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด 1๊ฐœ์˜ ํ•„๋“œ์— ๋Œ€ํ•ด ํ”„๋กœ์ ์…˜์„ ๊ฑธ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

Projection parameter

MongoDB์—์„œ find๋กœ query๋ฅผ ๊ฑธ์–ด์ค„ ๋•Œ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

db.$(collection name).find( {filter condition} , {projection parameter})

 

Example

๋‹ค์Œ์€ projectType์ด "GO"์ด๋ฉด์„œ, projectName๊ณผ, status ํ•„๋“œ๋งŒ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. 

db.collection.find({ projectType : "GO"}, {projectName : 1, status : 1})
๋ฐ˜์‘ํ˜•