MongoDB๋ ๋ฐ์ดํฐ ์กฐํ ์ ์ ๋ ฌ(Sort) ๊ธฐ๋ฅ์ ์ ๊ณตํ์ฌ ์ํ๋ ์์๋๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฆฌํ ์ ์์ต๋๋ค. ํนํ, ๋๋์ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋ ์ ๋ ฌ์ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ์ด๋ฒ ๊ธ์์๋ Node.js์ MongoDB๋ฅผ ์ฐ๋ํ์ฌ sort()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
Node.js์ MongoDB ์ฐ๋ ์ค์
MongoDB์์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํ๋ ค๋ฉด ๋จผ์ Node.js์ MongoDB๋ฅผ ์ฐ๋ํด์ผ ํฉ๋๋ค. ์ด๋ฅผ ์ํด mongodb
ํจํค์ง๋ฅผ ์ค์นํ๊ณ , MongoDB ์๋ฒ์ ์ฐ๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
1. MongoDB ์ค์น ๋ฐ ์ค์
MongoDB๋ ๋ก์ปฌ ํ๊ฒฝ์์ ์ค์นํ๊ฑฐ๋ MongoDB Atlas์ ๊ฐ์ ํด๋ผ์ฐ๋ ์๋น์ค๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ก์ปฌ์ MongoDB๋ฅผ ์ค์นํ๋ค๋ฉด, Node.js ํ๋ก์ ํธ์์ MongoDB์ ์ฐ๋ํ ์ ์๋๋ก mongodb
ํจํค์ง๋ฅผ ์ค์นํด์ผ ํฉ๋๋ค.
$ mkdir myapp
$ cd myapp
$ npm init -y
$ npm install mongodb
2. MongoDB ์ฐ๊ฒฐ ์ค์
๋ค์์ MongoDB ์๋ฒ์ ์ฐ๊ฒฐํ๋ ๊ธฐ๋ณธ์ ์ธ ์ฝ๋์
๋๋ค. MongoClient
๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ MongoDB์ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.
// app.js
const { MongoClient } = require('mongodb');
// MongoDB ์ฐ๊ฒฐ URL
const url = 'mongodb://localhost:27017'; // ๋ก์ปฌ MongoDB URL
const client = new MongoClient(url);
// ์ฌ์ฉํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๋ฆ
const dbName = 'myDatabase';
async function main() {
try {
// MongoDB ์ฐ๊ฒฐ
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
// ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ํ
const db = client.db(dbName);
console.log(`๋ฐ์ดํฐ๋ฒ ์ด์ค ${dbName} ์ ํ๋จ.`);
} catch (error) {
console.error('MongoDB ์ฐ๊ฒฐ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
// MongoDB ์ฐ๊ฒฐ ์ข
๋ฃ
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
main();
์ ์ฝ๋๋ฅผ ํตํด MongoDB ์๋ฒ์ ์ฐ๊ฒฐํ๊ณ , ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ ํํ ์ ์์ต๋๋ค. ์ด์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํ ์ค๋น๊ฐ ์๋ฃ๋์์ต๋๋ค.
MongoDB์์ ๋ฐ์ดํฐ ์ ๋ ฌํ๊ธฐ: sort()
๋ฉ์๋
MongoDB์์ sort()
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์กฐํ๋ ๋ฐ์ดํฐ๋ฅผ ํน์ ํ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ๋๋ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ ์ ์์ต๋๋ค. ์ ๋ ฌ์ 1๊ณผ -1๋ก ์ง์ ํ๋ฉฐ, 1
์ ์ค๋ฆ์ฐจ์(์์ ๊ฐ์์ ํฐ ๊ฐ ์), -1
์ ๋ด๋ฆผ์ฐจ์(ํฐ ๊ฐ์์ ์์ ๊ฐ ์)์ ์๋ฏธํฉ๋๋ค.
1. ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํฐ ์ ๋ ฌ
๋จผ์ users
์ปฌ๋ ์
์์ ์ฌ์ฉ์์ ๋์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ์์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
// ๋์ด ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
async function sortByAgeAscending() {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
// ๋์ด ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
const users = await collection.find({}).sort({ age: 1 }).toArray();
console.log('๋์ด ๊ธฐ์ค ์ค๋ฆ์ฐจ์ ์ ๋ ฌ๋ ์ฌ์ฉ์:', users);
} catch (error) {
console.error('๋ฐ์ดํฐ ์ ๋ ฌ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
sortByAgeAscending();
์ ์ฝ๋๋ users
์ปฌ๋ ์
์ ๋ฐ์ดํฐ๋ฅผ age
ํ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ์์์
๋๋ค. sort({ age: 1 })
์์ 1
์ ์ค๋ฆ์ฐจ์์ ์๋ฏธํฉ๋๋ค.
2. ๋ด๋ฆผ์ฐจ์์ผ๋ก ๋ฐ์ดํฐ ์ ๋ ฌํ๊ธฐ
์ด๋ฒ์๋ users
์ปฌ๋ ์
์์ ๋์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
// ๋์ด ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
async function sortByAgeDescending() {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
// ๋์ด ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
const users = await collection.find({}).sort({ age: -1 }).toArray();
console.log('๋์ด ๊ธฐ์ค ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ๋ ์ฌ์ฉ์:', users);
} catch (error) {
console.error('๋ฐ์ดํฐ ์ ๋ ฌ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
sortByAgeDescending();
์ ์ฝ๋๋ ๋์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ์์์
๋๋ค. sort({ age: -1 })
์์ -1
์ ๋ด๋ฆผ์ฐจ์์ ์๋ฏธํฉ๋๋ค.
3. ์ฌ๋ฌ ํ๋๋ก ๋ฐ์ดํฐ ์ ๋ ฌํ๊ธฐ
๋๋ก๋ ํ๋ ์ด์์ ํ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํด์ผ ํ ๋๊ฐ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๋์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ , ๋์ด๊ฐ ๊ฐ์ ๊ฒฝ์ฐ ์ด๋ฆ์ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
// ๋์ด ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์, ์ด๋ฆ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
async function sortByMultipleFields() {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
// ๋์ด ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์, ์ด๋ฆ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
const users = await collection.find({}).sort({ age: 1, name: -1 }).toArray();
console.log('๋์ด ๊ธฐ์ค ์ค๋ฆ์ฐจ์, ์ด๋ฆ ๊ธฐ์ค ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ๋ ์ฌ์ฉ์:', users);
} catch (error) {
console.error('๋ฐ์ดํฐ ์ ๋ ฌ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
sortByMultipleFields();
์ ์ฝ๋๋ ๋จผ์ age
๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ , age
๊ฐ์ด ๋์ผํ ๊ฒฝ์ฐ name
์ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ์์์
๋๋ค. sort({ age: 1, name: -1 })
๋ ๋ ๊ฐ์ง ํ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
4. ์ ๋ ฌ๊ณผ ์ ํ ํจ๊ป ์ฌ์ฉํ๊ธฐ
์ ๋ ฌ๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ ๋ ์์ ๋ช ๊ฐ์ ๊ฒฐ๊ณผ๋ง ๊ฐ์ ธ์ค๊ณ ์ถ์ ์ ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ sort()
์ limit()
๋ฉ์๋๋ฅผ ํจ๊ป ์ฌ์ฉํ์ฌ ํน์ ๊ฐ์๋งํผ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๋์ด ์์ผ๋ก ์์ 3๋ช
์ ์ฌ์ฉ์๋ง ์กฐํํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
// ๋์ด ์์ผ๋ก ์์ 3๋ช
์ ์ฌ์ฉ์๋ง ์กฐํ
async function sortAndLimit() {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
// ๋์ด ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๊ณ ์์ 3๋ช
๋ง ์กฐํ
const users = await collection.find({}).sort({ age: 1 }).limit(3).toArray();
console.log('๋์ด ์์ผ๋ก ์ ๋ ฌ๋ ์์ 3๋ช
์ ์ฌ์ฉ์:', users);
} catch (error) {
console.error('๋ฐ์ดํฐ ์ ๋ ฌ ๋ฐ ์ ํ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
sortAndLimit();
์ ์ฝ๋๋ ๋์ด ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ ํ ์์ 3๋ช
์ ์ฌ์ฉ์๋ง ์กฐํํ๋ ์์์
๋๋ค. limit(3)
๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์กฐํ ๊ฒฐ๊ณผ์ ๊ฐ์๋ฅผ ์ ํํ ์ ์์ต๋๋ค.
MongoDB ์ฐ๊ฒฐ ์ข ๋ฃ
๋ฐ์ดํฐ ์ ๋ ฌ ์์
์ด ๋๋ ํ์๋ MongoDB ์ฐ๊ฒฐ์ ์์ ํ๊ฒ ์ข
๋ฃํด์ผ ํฉ๋๋ค. client.close()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์ฐ๊ฒฐ์ ์ข
๋ฃํ ์ ์์ต๋๋ค.
// MongoDB ์ฐ๊ฒฐ ์ข
๋ฃ
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
๊ฒฐ๋ก
์ด๋ฒ ๊ธ์์๋ Node.js์ MongoDB๋ฅผ ์ฌ์ฉํ์ฌ sort()
๋ฉ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์์ต๋๋ค. MongoDB์ ์ ๋ ฌ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ๋ฅผ ํน์ ๊ธฐ์ค์ ๋ง์ถฐ ํจ์จ์ ์ผ๋ก ์ ๋ ฌํ ์ ์์ต๋๋ค. ์ฌ๋ฌ ํ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ฑฐ๋, ์ ๋ ฌ๋ ๋ฐ์ดํฐ์์ ์์ ๊ฒฐ๊ณผ๋ง ๊ฐ์ ธ์ค๋ ๋ฑ์ ๊ธฐ๋ฅ๋ ํจ๊ป ํ์ฉํ์ฌ ๊ฐ๋ ฅํ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. ์ด ๊ฐ์ด๋๋ฅผ ๋ฐํ์ผ๋ก MongoDB์์ ๋ฐ์ดํฐ๋ฅผ ํจ๊ณผ์ ์ผ๋ก ์ ๋ ฌํด๋ณด์ธ์!
๋๊ธ ์ฐ๊ธฐ