MongoDB๋ ๋์ฉ๋ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋, ์กฐํํ๋ ๋ฐ์ดํฐ์ ์์ ์ ์ดํ๊ธฐ ์ํด limit() ๋ฉ์๋๋ฅผ ์ ๊ณตํฉ๋๋ค. ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์์ ๋ฐํํ ๋ฐ์ดํฐ์ ๊ฐ์๋ฅผ ์ ํํ ์ ์์ต๋๋ค. ์ด๋ฒ ๊ธ์์๋ Node.js์ MongoDB๋ฅผ ์ฐ๋ํ์ฌ limit()
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
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์ Node.js๋ฅผ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.
// 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์์ ๋ฐ์ดํฐ ์ ํํ๊ธฐ: limit()
์ฌ์ฉ๋ฒ
MongoDB์์ limit()
๋ฉ์๋๋ ์กฐํ ๊ฒฐ๊ณผ์์ ๋ฐํํ ๋ฌธ์์ ๊ฐ์๋ฅผ ์ ํํ ๋ ์ฌ์ฉ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๋๋์ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋ ์ฒ์ ๋ช ๊ฐ์ ๋ฐ์ดํฐ๋ง ๊ฐ์ ธ์ค๊ณ ์ถ์ ๋ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก find()
๋ฉ์๋์ ํจ๊ป ์ฌ์ฉ๋ฉ๋๋ค.
1. ์์ 5๊ฐ์ ๋ฐ์ดํฐ ์กฐํ
๋ค์์ users
์ปฌ๋ ์
์์ ์์ 5๋ช
์ ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ๋ฐฉ๋ฒ์
๋๋ค.
// ์์ 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();
์ ์ฝ๋๋ users
์ปฌ๋ ์
์์ ์์ 5๋ช
์ ์ฌ์ฉ์๋ง ์กฐํํ๋ ์์์
๋๋ค. limit(5)
๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐํํ ๋ฐ์ดํฐ์ ๊ฐ์๋ฅผ 5๊ฐ๋ก ์ ํํฉ๋๋ค.
2. ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ ์ ํํ์ฌ ์กฐํํ๊ธฐ
ํน์ ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ์ ํํ์ฌ ์กฐํํ ์๋ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๋์ด๊ฐ 30 ์ด์์ธ ์ฌ์ฉ์ ์ค ์์ 3๋ช ์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
// ๋์ด๊ฐ 30 ์ด์์ธ ์ฌ์ฉ์ ์ค ์์ 3๋ช
์กฐํ
async function findFilteredAndLimitedData() {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
// ๋์ด๊ฐ 30 ์ด์์ธ ์ฌ์ฉ์ ์ค ์์ 3๋ช
๋ง ์กฐํ
const users = await collection.find({ age: { $gte: 30 } }).limit(3).toArray();
console.log('๋์ด๊ฐ 30 ์ด์์ธ ์์ 3๋ช
์ ์ฌ์ฉ์:', users);
} catch (error) {
console.error('๋ฐ์ดํฐ ์กฐํ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
findFilteredAndLimitedData();
์ ์ฝ๋๋ ๋์ด๊ฐ 30 ์ด์์ธ ์ฌ์ฉ์ ์ค ์์ 3๋ช
์ ๋ฐ์ดํฐ๋ง ์กฐํํ๋ ์์์
๋๋ค. limit(3)
๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ ์ค ์์ 3๊ฐ์ ๋ฌธ์๋ง ๋ฐํํฉ๋๋ค.
3. ์ ๋ ฌ๊ณผ ํจ๊ป ๋ฐ์ดํฐ ์ ํํ๊ธฐ
limit()
๋ฉ์๋๋ ์ ๋ ฌ๊ณผ ํจ๊ป ์ฌ์ฉํ ๋ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๋์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ๋ ์ฌ์ฉ์ ์ค ์์ 5๋ช
์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
// ๋์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ๋ ์์ 5๋ช
์ ์ฌ์ฉ์ ์กฐํ
async function findSortedAndLimitedData() {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
// ๋์ด ๊ธฐ์ค ์ค๋ฆ์ฐจ์ ์ ๋ ฌ ํ ์์ 5๋ช
๋ง ์กฐํ
const users = await collection.find({}).sort({ age: 1 }).limit(5).toArray();
console.log('๋์ด ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ๋ ์์ 5๋ช
์ ์ฌ์ฉ์:', users);
} catch (error) {
console.error('๋ฐ์ดํฐ ์กฐํ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
findSortedAndLimitedData();
์ ์ฝ๋๋ ๋์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ ํ ์์ 5๋ช
์ ์ฌ์ฉ์๋ง ์กฐํํ๋ ์์์
๋๋ค. sort()
์ limit()
๋ฉ์๋๋ฅผ ํจ๊ป ์ฌ์ฉํ์ฌ ์ ๋ ฌ๋ ๋ฐ์ดํฐ ์ค ์ผ๋ถ๋ง ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
4. ํ์ด์ง๋ค์ด์
๊ตฌํ: limit()
์ skip()
limit()
๋ฉ์๋๋ ํ์ด์ง๋ค์ด์
(pagination)์ ๊ตฌํํ ๋๋ ์ ์ฉํฉ๋๋ค. ํ์ด์ง๋ค์ด์
์ ๋ฐ์ดํฐ๋ฅผ ์ฌ๋ฌ ํ์ด์ง๋ก ๋๋์ด ์กฐํํ๋ ๋ฐฉ์์
๋๋ค. ์๋ฅผ ๋ค์ด, ํ ํ์ด์ง์ 5๋ช
์ ๋ฐ์ดํฐ๋ฅผ ํ์ํ๋ ํ์ด์ง๋ค์ด์
์ ๊ตฌํํ๋ ค๋ฉด skip()
๋ฉ์๋์ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค.
// ํ์ด์ง๋ค์ด์
๊ตฌํ ์์
async function findPaginatedData(page) {
try {
await client.connect();
console.log('MongoDB์ ์ฑ๊ณต์ ์ผ๋ก ์ฐ๊ฒฐ๋์์ต๋๋ค.');
const db = client.db(dbName);
const collection = db.collection('users');
const limit = 5; // ํ ํ์ด์ง์ 5๊ฐ์ ๋ฌธ์ ํ์
const skip = (page - 1) * limit; // ํ์ด์ง ๋ฒํธ์ ๋ฐ๋ฅธ ๋ฐ์ดํฐ ๊ฑด๋๋ฐ๊ธฐ
// ํ์ด์ง๋ค์ด์
์ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ ์กฐํ
const users = await collection.find({}).skip(skip).limit(limit).toArray();
console.log(`ํ์ด์ง ${page}์ ์ฌ์ฉ์ ๋ฐ์ดํฐ:`, users);
} catch (error) {
console.error('ํ์ด์ง๋ค์ด์
์กฐํ ์ค ์ค๋ฅ ๋ฐ์:', error);
} finally {
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
}
}
// ์์๋ก ํ์ด์ง 2์ ๋ฐ์ดํฐ๋ฅผ ์กฐํ
findPaginatedData(2);
์ ์ฝ๋๋ ํ์ด์ง๋ค์ด์
์ ๊ตฌํํ์ฌ ํ์ด์ง 2์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ์์์
๋๋ค. skip()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด ํ์ด์ง ๋ฒํธ์ ๋ฐ๋ผ ๊ฑด๋๋ธ ๋ฐ์ดํฐ๋ฅผ ์ค์ ํ๊ณ , limit()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด ํ ํ์ด์ง์ ํ์ํ ๋ฐ์ดํฐ ๊ฐ์๋ฅผ ์ ํํฉ๋๋ค.
MongoDB ์ฐ๊ฒฐ ์ข ๋ฃ
๋ฐ์ดํฐ ์กฐํ ์์
์ด ๋๋ ํ์๋ MongoDB ์ฐ๊ฒฐ์ ์์ ํ๊ฒ ์ข
๋ฃํด์ผ ํฉ๋๋ค. client.close()
๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์ฐ๊ฒฐ์ ์ข
๋ฃํ ์ ์์ต๋๋ค.
// MongoDB ์ฐ๊ฒฐ ์ข
๋ฃ
await client.close();
console.log('MongoDB ์ฐ๊ฒฐ์ด ์ข
๋ฃ๋์์ต๋๋ค.');
๊ฒฐ๋ก
์ด๋ฒ ๊ธ์์๋ Node.js์ MongoDB๋ฅผ ์ฌ์ฉํ์ฌ limit()
๋ฉ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ์ ํํ์ฌ ์กฐํํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด์์ต๋๋ค. limit()
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์กฐํํ ๋ฐ์ดํฐ์ ๊ฐ์๋ฅผ ์ ํํ์ฌ ์ฑ๋ฅ์ ์ต์ ํํ ์ ์์ผ๋ฉฐ, ์ ๋ ฌ๊ณผ ํจ๊ป ์ฌ์ฉํ๊ฑฐ๋ ํ์ด์ง๋ค์ด์
์ ๊ตฌํํ ๋๋ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ์ด ๊ฐ์ด๋๋ฅผ ๋ฐํ์ผ๋ก MongoDB์์ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ์กฐํํ๊ณ ๊ด๋ฆฌํด๋ณด์ธ์!
๋๊ธ ์ฐ๊ธฐ