DB 복습 7 (MongoDB- 비관계형DB)
SQL : Structed Query Language
- 관계형 데이터베이스 시스템에서 표준으로 사용되는 질의어 (부모-자식, Primary-)
- Oracle, MySQL, ... SQL을 사용하여 데이터를 관리함
- 테이블, 행, 열
- 한 행에 한개의 엔티티 속성을 저장
NoSQL :
- MongoDB, MariaDB : SQL을 사용하지 않고 JSON, Javascript 형태의 명령
- Collection (Table 같은 거 - 여러개의 엔티티를 저장할 수 있는 집합)
- Document (Row, 열)
- JSON 형식의 문서를 저장
예) { 'id' : 'S210', 'email' : 'smith@daum.net' }
- MongoDB 다운 : www.mongodb.com
-> MongoDB는 JS 언어 사용
-DB선택
use GameDB(DB명)
- 콜렉션 확인
show collections 또는
db.Player.find() 또는
db.Player.find({ name = 'king' })
- 추가하기
db.Player.insert({ "Name" : "Jupiter", "Team" : "TeamB", "Score" : 10, "Date" : ISODate("2011-08-27T15:00:00Z")} )
WriteResult({ "nInserted" : 1 })
- 수정하기
db.Player.update( {name : "Jupiter"}, {$set : {Score :35}} )
언리얼 위젯 버튼을 누르면
- 플레이어의, 팀명, 게임날짜, 점수를 MongoDB의 Player 컬렉션에 저장
- 언리얼(클라이언트측)에서는 VaRest 플러그인과 POST 전송방식을 이용
서버측에서는 Node.js의 Express 모듈을 사용한다
mongoDB를 실행하면 'mongoDB://localhost:27017'인 url로 접속한다 (http가 아니고 mongodb에 접속하는 것)
// 자료 추가하기
const insertDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('콜렉션 명');
// Insert some documents
collection.insertMany([
{ 'Key' : 'Value' ... }
예) {'Name' : 'Messi', 'Team' :'Barcelona','Score':88,'Date':'2008-11-12'},
{'Name' : 'NalGangDoo', 'Team' :'Maesootus','Score': 0,'Date':'2017-10-21'},
{'Name' : 'Lacazette', 'Team' :'Arsenal','Score':27,'Date':'2016-11-12'}
], function(err, result) {
assert.equal(err, null);
assert.equal(3, result.result.n);
assert.equal(3, result.ops.length);
callback(result);
});
}
// 자료 찾기
const findDocuments = function(db, callback) {
// Find some documents
const collection = db.collection('콜렉션 명');
collection.find({ Key : 'Value' }).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs)
callback(docs);
});
}
// 자료 수정
const updateDocument = function(db, callback) {
// Get the documents collection
const collection = db.collection('콜렉션 명');
// Update document where a is 2, set b equal to 1
collection.updateOne({ Key : 'Value' }
, { $set: { Key : 'Value' } }, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log("Updated the document with the field a equal to 2");
callback(result);
});
}
// 자료 삭제
const removeDocument = function(db, callback) {
// Get the documents collection
const collection = db.collection('콜렉션 명');
// Delete document where a is 3
collection.deleteOne({ Name : 'NalGangDoo' }, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log("Removed the document with the field a equal to 3");
callback(result);
});
}