MongoDB ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ํ๋ก์ ํธ ์ค์น ์ ์ด๋ฏธ ํจ๊ป ์ค์น ๋์ด์์ผ๋ฉฐ, meteor ๋ช ๋ น์ด๋ฅผ ํตํด ํ๋ก์ ํธ๋ฅผ ๊ตฌ๋ํ๋ ๋์์ MongoDB ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ญ์ ํจ๊ป ๊ตฌ๋๋ฉ๋๋ค. ํ๋ก์ ํธ์ ๊ตฌ๋ ์ ํฌํธ๋ฅผ ์ค์ ํ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก 3000๋ฒ์ ํฌํธ๋ฅผ ํ๋ก์ ํธ๋ฅผ ์ฌ์ฉํ๊ฒ ๋๊ณ , ๋ด์ฅ MongoDB๋ ์ฌ๊ธฐ์ 1์ ๋ํ 3001๋ฒ์์ ํจ๊ป ๊ตฌ๋ ๋ฉ๋๋ค.
[๊ธฐ๋ณธ API] MongoDB API ์ฐธ์กฐ
- Anywhere: new Meteor.Collection(name, [options]) # options: connection, idGeneration, transform
- Anywhere: collection.findAll(selector, [options]) # options: sort, skip, limit, fields, reactive, transform
- Anywhere: collection.findOne(selector, [options]) # options: sort, skip, fields, reactive, transform
- Anywhere: collection.insert(doc, [callback])
- Anywhere: collection.update(selector, modifier, [options], [callback]) # options: multi, upsert
- Anywhere: collection.remove(selector, [callback])
MongoDB์์๋ MySQL๊ณผ ๊ฐ์ ์ผ๋ฐ์ ์ธ RDBMS ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ํ ์ด๋ธ์ด๋ผ ๋ถ๋ฅด๋ ๋ฐ์ดํฐ ์ ์ ์ฝ๋ ์ (Collection)์ด๋ผ ๋ถ๋ฆ ๋๋ค. ํ๋ก์ ํธ ์ฝ๋ ๋ด์์ ์ด๋ฌํ ์ฝ๋ ์ ์ ์์ฑํ๊ธฐ ์ํด์ ํ๋ก์ ํธ ์ต์์ ๋๋ ํ ๋ฆฌ์ ์์นํ database.js ํ์ผ์ ์์ ํฉ๋๋ค.
Posts = new Mongo.Collection('posts')
์๊ธฐ ์ฝ๋๋ 'posts'๋ผ๋ ์ด๋ฆ์ ์ฝ๋ ์ ์ ์์ฑ ํ ํ, ์ด๋ฅผ Posts ๋ผ๋ ์ ์ญ ๋ณ์์ ํ ๋นํ์ฌ ์ ์ฒด ํ๋ก์ ํธ ์ด๋์์๋ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ํ๊ณ ์์ต๋๋ค.
์ฆ, ์ด์ Posts ๋ณ์๋ฅผ ํตํด ํด๋ผ์ด์ธํธ ํน์ ์๋ฒ์ js ํ์ผ ์ค ์ด๋์์๋ DB์ Posts ์ฝ๋ ์ ์ ์ ๊ทผํ์ฌ ์ฌ์ฉ ํ ์ ์์ต๋๋ค.
์์์ ํ ์คํธ Posts ์ฝ๋ ์ ์ ์์ฑ ํ๋ค๋ฉด, ์๋ ์ฝ๋๋ค์ server/main.js์ startup() ํจ์ ๋ด๋ถ์์ ํ ์คํธ ๊ฐ๋ฅํฉ๋๋ค. Meteor.startup(function() { // code to run on server at startup // ์๋ ์ฝ๋๋ค์ ์ด ๋ถ๋ถ์ ์ ๋ ฅํ๋ฉด์ ํ ์คํธ ํด ๋ณผ ์ ์์ต๋๋ค. });
์์ ์ด ์์ฑํ MongoDB ์ฝ๋ ์ ๋ณ์์ insert() ํจ์๋ฅผ ํตํด ์ํ๋ ํํ์ JSON๋ฐ์ดํฐ๋ฅผ ์ง์ ์ ์ฅํฉ๋๋ค.
var post_id = Posts.insert({
createdAt: new Date(), //์์ฑ๋ ์ง ๋ฑ ์ํ๋ ๊ตฌ์กฐ์ JSON ๋ฐ์ดํฐ
title: ...
...
});
์์ ๊ฐ์ด ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ ์, ๋๋คํ ๊ฐ์ UUID ๋ฐ์ดํฐ๊ฐ _id๋ผ๋ ๊ฐ์ผ๋ก JSON ๋ฐ์ดํฐ์ ์๋์ผ๋ก ํฌํจ๋์ด ์ ์ฅ ๋๋ฉฐ, ์ด๋ insert() ํจ์์ ๋ฐํ๊ฐ์ผ๋ก ์ ๋ฌ ๋์ด ํด๋น ๋ฐ์ดํฐ๋ฅผ ์๋ณํ๋ ์ ์ผํ Key๋ก ํฅํ ํด๋น ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ๋ ํ์ฉ ๊ฐ๋ฅ ํฉ๋๋ค.
๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ์ ์ ์ ์ฅํ๊ณ ๋๋ฉด ์ธ์ ๋ ํด๋น ๋ฐ์ดํฐ์ ์ ๊ทผ ํ ์ ์์ต๋๋ค. ํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋์ ๊ฐ์ ํ์์ ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋์ด ์๋ค๊ณ ๊ฐ์ ํ๊ณ ์ค๋ช ํฉ๋๋ค.
{ // ์ด๋ฌํ ํ์์ ๋ฐ์ดํฐ๊ฐ ๋ณต์๊ฐ ์ ์ฅ ๋์ด ์๋ค๊ณ ๊ฐ์ .
createdAt: new Date(),
title: 'Hello World',
message: 'Welcome to my world!'
writer: 'John Doe'
}
Posts.findOne({writer: 'John Doe'});
์๊ธฐ ์ฝ๋๋ ์ด๋ฆ์ด 'John Doe'์ธ ๋ฐ์ดํฐ๋ฅผ ์ ์ฒด ๊ฒ์ํ๊ธฐ ๋๋ฌธ์ writer ํญ๋ชฉ์ด 'John Doe'๋ก ๋์ด ์๋ ์ ์ฒด ๋ฌธ์์ ํด๋นํ๋ ์กฐ๊ฑด์ด์ง๋ง, findOne()์ 1๊ฐ์ ๋ฐ์ดํฐ๋ง ๋ฐํํ๊ธฐ ๋๋ฌธ์ ๊ฐ์ฅ ๋จผ์ ๊ฒ์ ๋ JSON ๋ฐ์ดํฐ๋ง ๋ฐํํ๊ฒ ๋ฉ๋๋ค.
๋ง์ฝ ์์์ ๋ณด์๋ insert() ์์ ์ ๊ฐ์ด post_id ๊ฐ์ ์ ์ ์๋ค๋ฉด, ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ์ฌ ์ ํํ๊ฒ ํด๋น ๋ฐ์ดํฐ๋ฅผ ์๋ณํ์ฌ ๊ฐ์ ธ์ฌ ์ ์์ ๊ฒ์ ๋๋ค. Posts.findOne({_id: post_id});
Posts.findAll({writer: 'John Doe'});
์๊ธฐ ์ฝ๋๋ ์ด๋ฆ์ด 'John Doe'์ธ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ๋์์ผ๋ก ํ๊ณ , ๊ทธ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ Array๋ก ๋ฐํํฉ๋๋ค.
find ๋ช ๋ น์ ํ๋ผ๋ฏธํฐ์ ์ฒซ๋ฒ์งธ Object๋ ์์ ๊ฐ์ด ๊ฒ์ ํญ๋ชฉ์ ๋ํ ์กฐ๊ฑด์ด๋ฉฐ, ๋๋ฒ์งธ Object๋ limit / skip / sort์ ๊ฐ์ ์ต์ ์ผ๋ก ๊ตฌ์ฑ ๊ฐ๋ฅํฉ๋๋ค.
Posts.findAll({writer: 'John Doe'}, {sort: {createdAt: -1}, skip: 20, limit: 10});
์๊ธฐ ์ฝ๋๋ ์ด๋ฆ์ด 'John Doe'์ธ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ๋์์ผ๋ก ๊ฒ์ ํ๋, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ createdAt ํญ๋ชฉ์ ์ญ์ ๋ ฌ(๋ ์ง์ ์ญ์ ๋ ฌ)ํ ํ, ์์์๋ถํฐ 20๊ฐ ํญ๋ชฉ์ ์ ์ธ(skip)ํ๊ณ , ์ดํ ๋ฐ์ดํฐ ์ค 10๊ฐ๋ง ์ ํ(limit)ํด์ ๊ฐ์ ธ์ค๋ ๋ช ๋ น์ ๋๋ค.
ํน์ ๋ฐ์ดํฐ์ ๊ฐ์ ์ ๋ฐ์ดํธ ํ ๋ ์ฌ์ฉํฉ๋๋ค. ํ๋ผ๋ฏธํฐ์ ์ฒซ๋ฒ์งธ Object๋ก ์กฐ๊ฑด์, ๋๋ฒ์งธ Object๋ก ๋ณํ ํ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ ํฉ๋๋ค.
var post_id = Posts.insert({...});
post = Posts.findOne({_id: post_id});
post.name = 'Jane Doe';
Posts.update({_id: post_id}, post);
์ ์ฝ๋๋ ์์์ ๋ฐ์ดํฐ๋ฅผ Posts์ ์ ์ฅ ํ ๋ฐํ ๋ _id๋ฅผ ์ด์ฉํ์ฌ ํด๋น ๋ฐ์ดํฐ ์ ์ฒด๋ฅผ ๋ค์ ๋ถ๋ฌ์จ ํ ํน์ ๊ฐ์ ๋ณ๊ฒฝํ์ฌ ๋ค์ ๊ฐ์ _id๋ก ์ ๋ฐ์ดํธ ํฉ๋๋ค.
ํน์ ๋ฐ์ดํฐ๋ฅผ ์ง์ธ ๋ ์ฌ์ฉํฉ๋๋ค. ํ๋ผ๋ฏธํฐ์ ์ฒซ๋ฒ์งธ Object๋ก ์กฐ๊ฑด์, ๋๋ฒ์งธ Object๋ก ์ผ์นํ๋ ๋ฐ์ดํฐ๊ฐ ๋ณต์ ์ผ ๊ฒฝ์ฐ ์ด๋ป๊ฒ ์ฒ๋ฆฌ ํ ์ง๋ฅผ ๊ฒฐ์ ํฉ๋๋ค.
Posts.remove({_id: post_id}); // post_id์ ํด๋นํ๋ ๋ฐ์ดํฐ๋ฅผ 1๊ฐ๋ง ์ง์๋๋ค.
Posts.remove({name: 'John Doe'}, {multi: true}); // nameํญ๋ชฉ์ด 'John Doe'์ธ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ์ง์๋๋ค.