Docs Menu
Docs Home
/
MongoDB ๋งค๋‰ด์–ผ
/ / /

์ด์ง„ ํฌ๊ธฐ(์ง‘๊ณ„)

์ด ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ

  • ์ •์˜
  • ํ–‰๋™
  • ์˜ˆ์‹œ
$binarySize

์ง€์ •๋œ ๋ฌธ์ž์—ด ๋˜๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ ๊ฐ’์˜ ์ฝ˜ํ…์ธ  ํฌ๊ธฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

$binarySize ์˜ ๊ตฌ๋ฌธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

{ $binarySize: <string or binData> }

์ธ์ˆ˜๋Š” ๋˜๋Š” ์ด์ง„ ๋ฐ์ดํ„ฐ ๊ฐ’์œผ๋กœ ํ•ด์„๋˜๋Š” ํ•œ ์œ ํšจํ•œ ํ‘œํ˜„์‹ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. string ํ‘œํ˜„์‹์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ํ‘œํ˜„์‹ ์—ฐ์‚ฐ์ž๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

$binarySize์˜ ์ธ์ˆ˜๋Š” ๋‹ค์Œ ์ค‘ ํ•˜๋‚˜๋กœ ํ•ด์„๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฌธ์ž์—ด,

  • ์ด์ง„ ๋ฐ์ดํ„ฐ ๊ฐ’ ๋˜๋Š”

  • null.

์ธ์ˆ˜๊ฐ€ ๋ฌธ์ž์—ด ๋˜๋Š” ์ด์ง„ ๋ฐ์ดํ„ฐ ๊ฐ’์ธ ๊ฒฝ์šฐ ํ‘œํ˜„์‹์€ ์ธ์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ธ์ˆ˜๊ฐ€ null ์ด๋ฉด ํ‘œํ˜„์‹์€ null ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ธ์ˆ˜๊ฐ€ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์œ ํ˜•์œผ๋กœ ํ™•์ธ๋˜๋ฉด $binarySize ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

$binarySize์˜ ์ธ์ˆ˜๊ฐ€ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ ์—ฐ์‚ฐ์ž๋Š” ๊ฐ ๋ฌธ์ž๊ฐ€ 1~4๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด์—์„œ UTF-8์œผ๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฐ”์ดํŠธ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ์˜ˆ์‹œ, US-ASCII ๋ฌธ์ž๋Š” 1๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค. ๋ถ„์Œ ๋ถ€ํ˜ธ์™€ ์ถ”๊ฐ€ ๋ผํ‹ด ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž(์˜์–ด ์•ŒํŒŒ๋ฒณ ์ด์™ธ์˜ ๋ผํ‹ด ๋ฌธ์ž)๊ฐ€ ์žˆ๋Š” ๋ฌธ์ž๋Š” 2๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค. ์ค‘๊ตญ์–ด, ์ผ๋ณธ์–ด ๋ฐ ํ•œ๊ตญ์–ด ๋ฌธ์ž์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ 3๋ฐ”์ดํŠธ๊ฐ€ ํ•„์š”ํ•˜๊ณ , ๋‹ค๋ฅธ ์œ ๋‹ˆ์ฝ”๋“œ ํ‰๋ฉด(์ด๋ชจํ‹ฐ์ฝ˜, ์ˆ˜ํ•™ ๊ธฐํ˜ธ ๋“ฑ)์—๋Š” 4๋ฐ”์ดํŠธ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ๋ฅผ ๊ณ ๋ คํ•˜์‹ญ์‹œ์˜ค.

์˜ˆ์‹œ
๊ฒฐ๊ณผ
์ฐธ๊ณ  ์‚ฌํ•ญ
{ $binarySize: "abcde" }

5

๊ฐ ๋ฌธ์ž๋Š” 1๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค.

{ $binarySize: "Hello World!" }

12

๊ฐ ๋ฌธ์ž๋Š” 1๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค.

{ $binarySize: "cafeteria" }

9

๊ฐ ๋ฌธ์ž๋Š” 1๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค.

{ $binarySize: "cafรฉtรฉria" }

11

รฉ ์€(๋Š”) 2๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค.

{ $binarySize: "" }

0

๋นˆ ๋ฌธ์ž์—ด์€ 0์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

{ $binarySize: "$โ‚ฌฮปG" }

7

โ‚ฌ ๋Š” 3๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค. ฮป ์€(๋Š”) 2๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค.

{ $binarySize: "ๅฏฟๅธ" }

6

๊ฐ ๋ฌธ์ž๋Š” 3๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉ๋ฉ๋‹ˆ๋‹ค.

mongosh์—์„œ ๋‹ค์Œ ๋ฌธ์„œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ images๋ผ๋Š” ์ด๋ฆ„์˜ ์ƒ˜ํ”Œ ์ปฌ๋ ‰์…˜์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

db.images.insertMany([
{ _id: 1, name: "cat.jpg", binary: new BinData(0, "OEJTfmD8twzaj/LPKLIVkA==")},
{ _id: 2, name: "big_ben.jpg", binary: new BinData(0, "aGVsZmRqYWZqYmxhaGJsYXJnYWZkYXJlcTU1NDE1Z2FmZCBmZGFmZGE=")},
{ _id: 3, name: "tea_set.jpg", binary: new BinData(0, "MyIRAFVEd2aImaq7zN3u/w==")},
{ _id: 4, name: "concert.jpg", binary: new BinData(0, "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=")},
{ _id: 5, name: "empty.jpg", binary: new BinData(0, "") }
])

๋‹ค์Œ ์• ๊ทธ๋ฆฌ๊ฒŒ์ด์…˜ projects:

  • name ํ•„๋“œ

  • imageSize ํ•„๋“œ(์ด ํ•„๋“œ๋Š” $binarySize๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ์˜ binary ํ•„๋“œ ํฌ๊ธฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•จ)

db.images.aggregate([
{
$project: {
"name": "$name",
"imageSize": { $binarySize: "$binary" }
}
}
])

์ด ์—ฐ์‚ฐ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

{ "_id" : 1, "name" : "cat.jpg", "imageSize" : 16 }
{ "_id" : 2, "name" : "big_ben.jpg", "imageSize" : 41 }
{ "_id" : 3, "name" : "teaset.jpg", "imageSize" : 16 }
{ "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 }
{ "_id" : 5, "name" : "empty.jpg", "imageSize" : 0 }

๋‹ค์Œ ํŒŒ์ดํ”„๋ผ์ธ์€ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ ํฌ๊ธฐ๊ฐ€ ๊ฐ€์žฅ ํฐ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

db.images.aggregate([
// First Stage
{ $project: { name: "$name", imageSize: { $binarySize: "$binary" } } },
// Second Stage
{ $sort: { "imageSize" : -1 } },
// Third Stage
{ $limit: 1 }
])
์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„

ํŒŒ์ดํ”„๋ผ์ธ projects ์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„์ž…๋‹ˆ๋‹ค.

  • name ํ•„๋“œ

  • imageSize ํ•„๋“œ(์ด ํ•„๋“œ๋Š” $binarySize๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ์˜ binary ํ•„๋“œ ํฌ๊ธฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•จ)

์ด ๋‹จ๊ณ„์—์„œ๋Š” ๋‹ค์Œ ๋ฌธ์„œ๋ฅผ ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

{ "_id" : 1, "name" : "cat.jpg", "imageSize" : 16 }
{ "_id" : 2, "name" : "big_ben.jpg", "imageSize" : 41 }
{ "_id" : 3, "name" : "teaset.jpg", "imageSize" : 16 }
{ "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 }
{ "_id" : 5, "name" : "empty.jpg", "imageSize" : 0 }
๋‘ ๋ฒˆ์งธ ๋‹จ๊ณ„

๋‘ ๋ฒˆ์งธ ๋‹จ๊ณ„์—์„œ๋Š” ๋ฌธ์„œ๋ฅผ imageSize ์”ฉ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ sorts ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์ด ๋‹จ๊ณ„์—์„œ๋Š” ๋‹ค์Œ ๋ฌธ์„œ๋ฅผ ๋‹ค์Œ ๋‹จ๊ณ„๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

{ "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 }
{ "_id" : 2, "name" : "big_ben.jpg", "imageSize" : 41 }
{ "_id" : 1, "name" : "cat.jpg", "imageSize" : 16 }
{ "_id" : 3, "name" : "teaset.jpg", "imageSize" : 16 }
{ "_id" : 5, "name" : "empty.jpg", "imageSize" : 0 }
์„ธ ๋ฒˆ์งธ ๋‹จ๊ณ„

์„ธ ๋ฒˆ์งธ ๋‹จ๊ณ„์—์„œ๋Š” ์ถœ๋ ฅ ๋ฌธ์„œ๋ฅผ limits ์ฒ˜๋ฆฌํ•˜์—ฌ ์ •๋ ฌ ์ˆœ์„œ์—์„œ ์ฒ˜์Œ์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์„œ๋งŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

{ "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 }

ํŒ

๋‹ค์Œ๋„ ์ฐธ์กฐํ•˜์„ธ์š”.

๋Œ์•„๊ฐ€๊ธฐ

$avg

์ด ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ