MongoDB๋ ๋์ฉ๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฐํ๊ฒ ์ ์ฅํ๊ณ ๊ด๋ฆฌํ ์ ์๋ NoSQL ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋๋ค. ํนํ, ๋ฐ์ดํฐ๋ฅผ ์กฐํํ ๋๋ find ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋งค์ฐ ํจ์จ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ ์ ์์ต๋๋ค. ์ด๋ฒ ๊ธ์์๋ Node.js์ MongoDB๋ฅผ ์ฐ๋ํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํ๊ฒ ์ต๋๋ค.
Node.js์ MongoDB ์ฐ๋ํ๊ธฐ
MongoDB์์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๊ธฐ ์ํด์๋ ๋จผ์ Node.js์ MongoDB๋ฅผ ์ฐ๋ํด์ผ ํฉ๋๋ค. ์ด๋ฅผ ์ํด mongodb
ํจํค์ง๋ฅผ ์ค์นํ๊ณ , MongoDB ์๋ฒ์ ์ฐ๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
1. MongoDB ์ค์น ๋ฐ ์ค์
MongoDB๋ ๋ก์ปฌ์์ ์ค์นํ ์ ์์ผ๋ฉฐ, ํด๋ผ์ฐ๋ ๊ธฐ๋ฐ์ MongoDB Atlas๋ฅผ ํตํด ํด๋ผ์ฐ๋์์ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ก์ปฌ์ MongoDB ์๋ฒ๋ฅผ ์ค์นํ๋ค๋ฉด, ์๋ ๋ช
๋ น์ด๋ก Node.js ํ๋ก์ ํธ๋ฅผ ์์ฑํ๊ณ 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();
์ ์ฝ๋๋ Node.js ์ ํ๋ฆฌ์ผ์ด์ ์์ MongoDB ์๋ฒ์ ์ฐ๊ฒฐํ๋ ๊ณผ์ ์ ๋๋ค. ์ด์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ ์ค๋น๊ฐ ๋์์ต๋๋ค.
MongoDB์์ ๋ฐ์ดํฐ ์กฐํํ๊ธฐ: find()
MongoDB์์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ๊ธฐ๋ณธ ๋ฐฉ๋ฒ์ find()
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค. find()
๋ ์ผ์นํ๋ ๋ฌธ์๋ค์ ๊ฒ์ํด ๋ฐฐ์ด๋ก ๋ฐํํ๋ฉฐ, ๋ค์ํ ์กฐ๊ฑด์ ์ค์ ํ์ฌ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ ์ ์์ต๋๋ค.
1. ๋ชจ๋ ๋ฐ์ดํฐ ์กฐํ: find()
์ปฌ๋ ์
์ ์๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ค๋ฉด ์กฐ๊ฑด ์์ด find()
๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๋ฉ๋๋ค. ๋ค์์ users
์ปฌ๋ ์
์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ์์์
๋๋ค.
// ๋ชจ๋ ๋ฐ์ดํฐ ์กฐํ ์์
async function findAllData() {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
// ๋ชจ๋ ์ฌ์ฉ์ ์กฐํ
const users = await collection.find({}).toArray();
console.log('์กฐํ๋ ์ฌ์ฉ์:', users);
} catch (error) {
console.error('๋ฐ์ดํฐ ์กฐํ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
findAllData();
์ ์ฝ๋๋ users
์ปฌ๋ ์
์ ๋ชจ๋ ๋ฌธ์๋ฅผ ์กฐํํ๋ ์์์
๋๋ค. find()
๋ฉ์๋๋ MongoDB์ ๋ฌธ์๋ค์ ๊ฒ์ํ๊ณ , toArray()
๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์ด๋ก ๋ณํํด ๋ฐํํฉ๋๋ค.
2. ํน์ ์กฐ๊ฑด์ผ๋ก ๋ฐ์ดํฐ ์กฐํ: find()
์ ํํฐ ์ฌ์ฉ
ํน์ ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ค๋ฉด find()
๋ฉ์๋์ ํํฐ ์กฐ๊ฑด์ ์ ๋ฌํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๋์ด๊ฐ 30 ์ด์์ธ ์ฌ์ฉ์๋ง ์กฐํํ๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
// ํน์ ์กฐ๊ฑด์ผ๋ก ๋ฐ์ดํฐ ์กฐํ
async function findFilteredData() {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
// ๋์ด๊ฐ 30 ์ด์์ธ ์ฌ์ฉ์ ์กฐํ
const users = await collection.find({ age: { $gte: 30 } }).toArray();
console.log('๋์ด๊ฐ 30 ์ด์์ธ ์ฌ์ฉ์:', users);
} catch (error) {
console.error('๋ฐ์ดํฐ ์กฐํ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
findFilteredData();
์ ์ฝ๋๋ find()
๋ฉ์๋์ ์กฐ๊ฑด์ ์ ๋ฌํ์ฌ ๋์ด๊ฐ 30 ์ด์์ธ ์ฌ์ฉ์๋ง ์กฐํํ๋ ์์์
๋๋ค. MongoDB์ ์กฐ๊ฑด ์ฐ์ฐ์์ธ $gte
๋ "ํฌ๊ฑฐ๋ ๊ฐ๋ค"๋ ์๋ฏธ๋ฅผ ๊ฐ์ต๋๋ค.
3. ํน์ ํ๋๋ง ์กฐํํ๊ธฐ: find()
์ projection ์ฌ์ฉ
ํน์ ํ๋๋ง ์กฐํํ๋ ค๋ฉด find()
๋ฉ์๋์์ projection์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ์ฌ์ฉ์ ์ด๋ฆ๊ณผ ๋์ด๋ง ์กฐํํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์์ต๋๋ค.
// ํน์ ํ๋๋ง ์กฐํ
async function findSpecificFields() {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
// ์ฌ์ฉ์ ์ด๋ฆ๊ณผ ๋์ด๋ง ์กฐํ
const users = await collection.find({}, { projection: { name: 1, age: 1, _id: 0 } }).toArray();
console.log('์กฐํ๋ ์ฌ์ฉ์ (์ด๋ฆ, ๋์ด):', users);
} catch (error) {
console.error('๋ฐ์ดํฐ ์กฐํ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
findSpecificFields();
์ ์ฝ๋๋ projection
์ ์ฌ์ฉํ์ฌ name
๊ณผ age
ํ๋๋ง ์กฐํํ๊ณ , ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐํ๋๋ _id
ํ๋๋ ์ ์ธํฉ๋๋ค. 1
์ ํ๋๋ฅผ ํฌํจํ๋ผ๋ ์๋ฏธ์ด๋ฉฐ, 0
์ ์ ์ธ๋ฅผ ์๋ฏธํฉ๋๋ค.
4. ๋ฐ์ดํฐ ์ ๋ ฌํ๊ธฐ: sort()
๋ฐ์ดํฐ๋ฅผ ์กฐํํ ๋ ์ ๋ ฌ์ด ํ์ํ ๊ฒฝ์ฐ sort()
๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๋์ด ์์ผ๋ก ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํฉ๋๋ค.
// ๋ฐ์ดํฐ ์ ๋ ฌ ์์
async function findSortedData() {
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 ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
findSortedData();
์ ์ฝ๋๋ ๋์ด ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์(1)์ผ๋ก ์ ๋ ฌํ ํ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ์์์
๋๋ค. ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ ค๋ฉด 1
๋์ -1
์ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
5. ์กฐํ ๊ฒฐ๊ณผ ์ ํํ๊ธฐ: limit()
์ฌ์ฉ
๋ง์ ๋ฐ์ดํฐ ์ค ์ผ๋ถ๋ง ์กฐํํ๋ ค๋ฉด limit()
๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ์์ 5๊ฐ์ ๋ฐ์ดํฐ๋ง ๊ฐ์ ธ์ค๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์์ต๋๋ค.
// ์กฐํ ๊ฒฐ๊ณผ ์ ํ ์์
async function findLimitedData() {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
// ์์ 5๊ฐ์ ์ฌ์ฉ์๋ง ์กฐํ
const users = await collection.find({}).limit(5).toArray();
console.log('์์ 5๋ช
์ ์ฌ์ฉ์:', users);
} catch (error) {
console.error('๋ฐ์ดํฐ ์กฐํ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
findLimitedData();
์ ์ฝ๋๋ limit()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์์ 5๊ฐ์ ๋ฌธ์๋ง ๊ฐ์ ธ์ค๋ ์์์
๋๋ค.
MongoDB ์ฐ๊ฒฐ ์ข ๋ฃ
๋ฐ์ดํฐ ์กฐํ ์์
์ด ๋๋ ํ์๋ MongoDB ์ฐ๊ฒฐ์ ์์ ํ๊ฒ ์ข
๋ฃํด์ผ ํฉ๋๋ค. client.close()
๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ MongoDB ์ฐ๊ฒฐ์ ์ข
๋ฃํ ์ ์์ต๋๋ค.
// MongoDB ์ฐ๊ฒฐ ์ข
๋ฃ
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
๊ฒฐ๋ก
์ด๋ฒ ๊ธ์์๋ Node.js์ MongoDB๋ฅผ ์ฌ์ฉํ์ฌ find()
๋ฉ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด์์ต๋๋ค. find()
๋ฉ์๋๋ MongoDB์์ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ ๋ ๋งค์ฐ ๊ฐ๋ ฅํ ๋๊ตฌ๋ก, ๋ค์ํ ์กฐ๊ฑด๊ณผ ์ ๋ ฌ, ํ๋ ์ ํ ๊ธฐ๋ฅ์ ํตํด ์ ์ฐํ ๋ฐ์ดํฐ ์กฐํ๋ฅผ ์ง์ํฉ๋๋ค. ์ด ๊ฐ์ด๋๋ฅผ ๋ฐํ์ผ๋ก MongoDB์์ ๋ฐ์ดํฐ๋ฅผ ํจ๊ณผ์ ์ผ๋ก ์กฐํํ๊ณ ๊ด๋ฆฌํ๋ ์ ํ๋ฆฌ์ผ์ด์
์ ๊ฐ๋ฐํด๋ณด์ธ์!
๋๊ธ ์ฐ๊ธฐ