๐ก MongoDB ๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์๋ค.
์ฐธ๊ณ : https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/
Project Fields to Return from Query — MongoDB Manual
Docs Home → MongoDB Manualโค Use the Select your language drop-down menu in the upper-right to set the language of the following examples.By default, queries in MongoDB return all fields in matching documents. To limit the amount of data that MongoDB se
docs.mongodb.com
Query์์ ๋ฐํํ ํ๋ก์ ํธ field
inventory collection ๊ธฐ๋ฐ์ผ๋ก ์์ ๋ฅผ ์งํํ๋ค.
db.inventory.insertMany( [
{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
๊ธฐ๋ณธ์ ์ผ๋ก MongoDB์ ์ฟผ๋ฆฌ๋ ์ผ์นํ๋ document์ ๋ชจ๋ field๋ฅผ ๋ฐํํ๋ค. MongoDB๊ฐ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ณด๋ด๋ ๋ฐ์ดํฐ์ ์์ ์ ํํ๊ธฐ ์ํด projection document๋ฅผ ํฌํจํ์ฌ ๋ฐํํ field๋ฅผ ์ง์ ํ๊ฑฐ๋ ์ ํํ ์ ์๋ค.
projection ๊ฐ๋ ์ฐธ์กฐ : http://www.gurubee.net/lecture/2176
SELECT ๊ธฐ์ด
SELECT ๋ฌธ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๊ตฌ๋ฌธ์ ๋๋ค. SELECT ๋ฌธ์ ์ด์ฉํด ์ง์ํ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์..
www.gurubee.net
์ผ์นํ๋ document์ ๋ชจ๋ field ๋ฐํ
- ex) status : “A”์ ์ผ์นํ๋ inventory collection์ ๋ชจ๋ document์์ ๋ชจ๋ field๋ฅผ ๋ฐํํ๋ค.
Mongodb shell
db.inventory.find( { status: "A" } )
SQL
SELECT * from inventory WHERE status = "A"
์ง์ ๋ field ๋ฐ _id field๋ง ๋ฐํ
- ex) item, status ๊ทธ๋ฆฌ๊ณ _id field๋ง ์ผ์นํ๋ document๋ฅผ ๋ฐํํ๋ค.
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
SQL
SELECT _id, item, status from inventory WHERE status = "A"
- _id ํ๋ ์ต์
- ex) ํ๋ก์ ์ ์์ _id ํ๋๋ฅผ 0์ผ๋ก ์ค์ ํ์ฌ ๊ฒฐ๊ณผ์์ ํ๋๋ฅผ ์ ๊ฑฐํ ์ ์๋ค.
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
SQL
SELECT _id, item, status from inventory WHERE status = "A"
Memo
: _id ํ๋๋ฅผ ์ ์ธํ๊ณ projection document์ ํฌํจ ๋ฐ ์ ์ธ๋ฌธ์ ๊ฒฐํฉํ ์ ์๋ค.
์ ์ธ๋ field๋ฅผ ์ ์ธํ ๋ชจ๋ ํญ๋ชฉ ๋ฐํ
์ผ์นํ๋ document์์ ๋ฐํํ field๋ฅผ ๋์ดํ๋ ๋์ projection์ ์ฌ์ฉํ์ฌ ํน์ ํ๋๋ฅผ ์ ์ธํ ์ ์๋ค.
- ex) ์ผ์นํ๋ document์ status: “A”์ ํด๋นํ์ง๋ง status์ instock์ ์ ์ธํ ๋ชจ๋ ํ๋๋ฅผ ๋ฐํํ๋ค.
ํฌํจ๋ document์ ํน์ field ๋ฐํ
ํฌํจ๋ document์ ํน์ field๋ฅผ ๋ฐํํ ์ ์๋ค. dot notation(์ ํ๊ธฐ๋ฒ)์ ์ฌ์ฉํ์ฌ ํฌํจ๋ ํ๋๋ฅผ ์ฐธ์กฐํ๊ณ projection document์ 1๋ก ์ค์ ํ๋ฉด ๋๋ค.
- ex) ๋ฐํํ๋ field : _id, item, status, size์ uom
db.inventory.find(
{ status: "A" },
{ item: 1, status: 1, "size.uom": 1 }
)
Memo :
MongoDB 4.4๋ถํฐ ์ค์ฒฉ ํ์์ ์ฌ์ฉํด ํฌํจ๋ field๋ฅผ ์ง์ ํ ์ ์๋ค.
ex) { item: 1, status: 1, size: { uom: 1 } }.
ํฌํจ๋ document์์ ํน์ field ์ต์
ํฌํจ๋ document์์ ํน์ field๋ฅผ ํ์ํ์ง ์์ ์ ์๋ค. dot notation(์ ํ๊ธฐ๋ฒ)์ ์ฌ์ฉํด projection document์ ํฌํจ๋ ํ๋๋ฅผ ์ฐธ์กฐํ๊ณ 0์ผ๋ก ์ค์ ํ๋ค.
- ex) uom์์ size document ๋ด๋ถ์ ํ๋๋ฅผ ์ ์ธํ๋๋ก projection์ ์ง์ ํ๋ค. ๋ค๋ฅธ ๋ชจ๋ field๋ ์ผ์นํ๋ document์ ๋ฐํ๋๋ค.
db.inventory.find(
{ status: "A" },
{ "size.uom": 0 }
)
Memo :
MongoDB 4.4๋ถํฐ ์ค์ฒฉ ํ์์ ์ฌ์ฉํ์ฌ ํฌํจ๋ field๋ฅผ ์ง์ ํ ์ ์๋ค.
ex) :{ size: { uom: 0 } }
๋ฐฐ์ด์ ํฌํจ๋ document์ ๋ํ projection
dot notation(์ ํ๊ธฐ๋ฒ)์ ์ฌ์ฉํด ํฌํจ๋ document ๋ด๋ถ์ ํน์ field๋ฅผ projectionํ๋ค.
- ex) ๋ฐํํ projection ์ง์ → _id, item, status, instock ๋ฐฐ์ด์ ํฌํจ๋ qty ํ๋
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )
๋ฐํ๋ ๋ฐฐ์ด์ ํน์ ๋ฐฐ์ด ์์๋ฅผ projection
๋ฐฐ์ด์ ํฌํจํ๋ field์ ๊ฒฝ์ฐ, MongoDB๋ ๋ฐฐ์ด ์กฐ์์ ์ํด $eleMatch, $slice, $์ ๊ฐ์ ํ๋ก์ ์ ์ฐ์ฐ์๋ฅผ ์ ๊ณตํ๋ค.
- ex) $slice projection ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด instock ๋ฐฐ์ด์ ๋ง์ง๋ง ์์๋ฅผ ๋ฐํํ๋ค.
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
Memo :
$eleMatch, $slice, $๋ ๋ฐํ๋ ๋ฐฐ์ด์ ํฌํจํ ํน์ ์์๋ฅผ projectionํ๋ ์ ์ผํ ๋ฐฉ๋ฒ์ด๋ค.
๋ฐฐ์ด ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ์ฌ ํน์ ๋ฐฐ์ด ์์๋ฅผ ํฌ์ํ ์ ์๋ค.
ex) { "instock.0": 1 } projection์ ์ฒซ ๋ฒ์งธ ์์๊ฐ ์๋ ๋ฐฐ์ด์ projection ํ์ง ์๋๋ค.
'Database > MongoDB' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
MongoDB - ๋ฐฐ์ด ์ฟผ๋ฆฌ - (2) (0) | 2021.12.19 |
---|---|
MongoDB - ๋ฐฐ์ด ์ฟผ๋ฆฌ - (1) (0) | 2021.12.19 |
MongoDB - Document Query(์กฐํ) - (2) (1) | 2021.12.18 |
MongoDB - Document Query(์กฐํ) - (1) (0) | 2021.12.18 |
MongoDB - Database/Collection/Document์์ฑ,์ ๊ฑฐ (0) | 2021.12.18 |