191226 데이터베이스
MongoDB
명령어
mongod -dbpath [디렉토리]
DB 서버 가동.
db.stats() : 상태
db.createCollection("string") : 컬렉션 "string" 생성
db.[컬렉션 이름].validate() : 컬렉션 정보 출력
db.[컬렉션 이름].save({ key:"value" }) : 데이터 삽입.
save vs insert
"_id"가 중복일 경우 save는 값을 덮어 씌우고, insert는 오류 발생.
# employees 라는 컬렉션이 있다고 하자.
db.employees.find( object ) : object 검색.
ex) db.employees.find( {empno : {$gt:1105} } ) : empno가 1105보다 큰 객체를 찾아줌.
- MongoDB 연산자
$lt : 미만
$lte : 이하
$gt : 초과
$gte : 이상
$ne : Not Equal
$eq : Equal
$and, $or, $not : 그냥 and, or, not 처럼 쓰면 됨.
ex) db.employees.find( { $and : [ {empno:{$gt:1005}}, {empno:{$lt:1010}}] } );
empno 가 1005 초과, 1010 미만인 object 검색.
db.employees.update( objectA, objectB ) : objectA를 objectB로 수정
db.employees.remove( object ) : object 삭제
db.employees.drop() : 컬렉션 삭제
printjson( object ) : JSON 타입을 깔끔하게 출력해줌.
ex) while(cursor.hasNext()) printjson(cursor.next()); // 1회용..
n = { empno : 1101 }; : JSON 변수 선언. for 문도 사용 가능. 자바스크립트 문법을 쓸 수 있다.
- 레플리케이션 : 교재 199p