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

๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค

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

  • ๋ช…๋ น ๊ตฌ๋ฌธ
  • ํ–‰๋™
  • ์˜ˆ์‹œ
  • ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค ์ƒ์„ฑ
  • ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋Š” ์ปฌ๋ ‰์…˜์˜ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค

๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  string ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ง€์› ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์€ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์—์„œ ํŒŒ์ƒ๋ฉ๋‹ˆ๋‹ค.

์ค‘์š”

๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ $regex ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. $regex ๊ตฌํ˜„์€ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋ฉฐ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

collation ์˜ต์…˜์„ ์ง€์ •ํ•˜์—ฌ db.collection.createIndex()๋กœ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

db.collection.createIndex(
{
<field>: <sortOrder>
},
{
collation:
{
locale : <locale>,
strength : < 1 | 2 >
}
}
)

๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค์— ๋Œ€ํ•œ ๋Œ€์กฐ๋ฅผ ์ง€์ •ํ•˜๋ ค๋ฉด collation ๊ฐ์ฒด์— ๋‹ค์Œ ํ•„๋“œ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

ํ•„๋“œ
์„ค๋ช…

locale

์–ธ์–ด ๊ทœ์น™์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ตญ๊ฐ€ ๋ฐ ์–ธ์–ด ์„ค์ • ๋ชฉ๋ก์€ ์ง€์›๋˜๋Š” ๊ตญ๊ฐ€ ๋ฐ ์–ธ์–ด ์„ค์ •์„ ์ฐธ์กฐํ•˜์„ธ์š”.

strength

๋น„๊ต ๊ทœ์น™์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค. strength ๊ฐ’์ด 1 ๋˜๋Š” 2์ด๋ฉด ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ ์ •๋ ฌ ํ•„๋“œ์— ๋Œ€ํ•ด์„œ๋Š” ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„์ฐธ์กฐํ•˜์„ธ์š”.

๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ์ฟผ๋ฆฌ ๋ฐ ์ •๋ ฌ ์ž‘์—…์—์„œ ์ธ๋ฑ์Šค์™€ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ปฌ๋ ‰์…˜์— ๋ฐ์ดํ„ฐ ์ •๋ ฌ์ด ์ •์˜๋˜์–ด ์žˆ๋‹ค๋ฉด, ๋ชจ๋“  ์ฟผ๋ฆฌ์™€ ์ธ๋ฑ์Šค๋Š” ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š๋Š” ํ•œ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ƒ์†ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์ด ์—†๋Š” ์ปฌ๋ ‰์…˜์—์„œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ์ดํ„ฐ ์ •๋ ฌ์ด ์žˆ๋Š” ์ธ๋ฑ์Šค๋ฅผ ๋งŒ๋“ค๊ณ  strength ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ 1 ๋˜๋Š” 2๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค(strength ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์„ค๋ช…์€ ๋ฐ์ดํ„ฐ ์ •๋ ฌ ์ฐธ์กฐ). ์ธ๋ฑ์Šค ์ˆ˜์ค€ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ฟผ๋ฆฌ ์ˆ˜์ค€์—์„œ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ์—์„œ๋Š” ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์ด ์—†๋Š” ์ปฌ๋ ‰์…˜์„ ๋งŒ๋“  ๋‹ค์Œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ type ํ•„๋“œ์— ์ธ๋ฑ์Šค๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

db.createCollection("fruit")
db.fruit.createIndex(
{ type: 1 },
{ collation: { locale: 'en', strength: 2 } }
)

์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ฟผ๋ฆฌ์—์„œ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

db.fruit.insertMany( [
{ type: "apple" },
{ type: "Apple" },
{ type: "APPLE" }
] )
db.fruit.find( { type: "apple" } ) // does not use index, finds one result
db.fruit.find( { type: "apple" } ).collation( { locale: 'en', strength: 2 } )
// uses the index, finds three results
db.fruit.find( { type: "apple" } ).collation( { locale: 'en', strength: 1 } )
// does not use the index, finds three results

๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปฌ๋ ‰์…˜์„ ๋งŒ๋“ค ๊ฒฝ์šฐ, ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•˜์ง€ ์•Š๋Š” ํ•œ ์ดํ›„์— ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋“  ์ธ๋ฑ์Šค๊ฐ€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ƒ์†ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•˜์ง€ ์•Š๋Š” ๋ชจ๋“  ์ฟผ๋ฆฌ๋„ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ƒ์†ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ์—์„œ๋Š” names์ด๋ผ๋Š” ๊ธฐ๋ณธ๊ฐ’ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ปฌ๋ ‰์…˜์„ ๋งŒ๋“  ๋‹ค์Œ first_name ํ•„๋“œ์— ์ธ๋ฑ์Šค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

db.createCollection("names", { collation: { locale: 'en_US', strength: 2 } } )
db.names.createIndex( { first_name: 1 } ) // inherits the default collation

์ด๋ฆ„์˜ ์ž‘์€ ์ปฌ๋ ‰์…˜์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

db.names.insertMany( [
{ first_name: "Betsy" },
{ first_name: "BETSY"},
{ first_name: "betsy"}
] )

์ด ์ปฌ๋ ‰์…˜์— ๋Œ€ํ•œ ์ฟผ๋ฆฌ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์ •๋œ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์ธ๋ฑ์Šค๋„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

db.names.find( { first_name: "betsy" } )
// inherits the default collation: { collation: { locale: 'en_US', strength: 2 } }
// finds three results

์œ„ ์ž‘์—…์€ ์ปฌ๋ ‰์…˜์˜ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๊ณ  ์„ธ ๋ฌธ์„œ๋ฅผ ๋ชจ๋‘ ์ฐพ์Šต๋‹ˆ๋‹ค. ๋” ๋‚˜์€ ์„ฑ๋Šฅ์„ ์œ„ํ•ด first_name ํ•„๋“œ์˜ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ฟผ๋ฆฌ์— ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์ง€์ •ํ•˜์—ฌ ์ด ์ปฌ๋ ‰์…˜์—์„œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ฒ€์ƒ‰ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

db.names.find( { first_name: "betsy" } ).collation( { locale: 'en_US' } )
// does not use the collection's default collation, finds one result

์œ„ ์ž‘์—…์€ strength ๊ฐ’์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ ๋ฌธ์„œ๋งŒ ์ฐพ์Šต๋‹ˆ๋‹ค. ์ปฌ๋ ‰์…˜์˜ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์ •๋ ฌ์ด๋‚˜ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

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

์†์„ฑ