기본 콘텐츠로 건너뛰기

기본적인 MongoDB 사용/자바연동







mongoDB 명령어
db: 현재 사용 중인 데이터베이스 확인
-test :Default로 들어가 있는 test 테이블
show dbs : DB의 리스트 확인
show collections : collection 리스트 확인, collection = table in mongoDB
show tables : 위 명령어와 같음
db.stats() : 데이터베이스 상태 확인
db.(db명).drop(): 안에 있는 데이터 삭제
mongod --version => mongodb 확인
mongo -version => shell 확인



use 테이블 명: 테이블 생성

db.dropDatabase() : 접속된 테이블 삭제 -테이블 확인 필요!!

db.createCollection(“테이블 명”): 테이블 생성

db.테이블 명.drop(): 테이블 지움

db.blog[테이블 명].insert({“name ” : “ nodejs”}) : 내용(Document)을 추가


db.테이블 명.insert([ {“ ” : “ ”},{“ ” : “ ”}]);
-여러 Documents를 하나의 테이블에 넣는 방법


db.blog[테이블 명].find() : 내용 확인
db.테이블 명.find().pretty() : 보기 좋게 검색
id : primary key를 자동으로 부여해줌


db.테이블 명.remove({“name” : “book01”}) : 특정한 Documents를 지우는 법



insert 문




count 문


연산자

몽고디비 비교 연산자

$eq 2. $gt 3. $gte 4. $lt 5. $lte
ne : 주어진 값과 일치 하지 않는 값
in: 주어진 배열 안에 속하는 값



nin: 주어진 배열 안에 속하지 않는 값

몽고디비 논리연산자
1.$or: 주어진 조건 중 하나라도 true일 때 true



2.$and:주어진 모든 조건이 true일 때 true
=And 연산자만 , 로 조건을 나열해서 검색 가능

3.$not:주어진 조건이 false일 때 true
4.$nor:주어진 모든 조건이 false일 때 true

find 문
=db. 테이블 명.find({조건 } , {출력할 column })
첫번째 {}: where 조건
두번째 {}:매개변수

ID 없이 출력하는 조건식

ID없이 title과 likes 컬럼을 출력하는 조건식

join
=embedded documents
$slice


Sort
=sort({key : value})
key : 정렬할 컬럼이름
value : 1,-1

sort 조건 두개 이상
amount를 정렬하고 그 다음 중복된 값[amount:10 ]에 대해서 id값 내림차순정렬

limit
=limit을 걸어 3개만 출력

skip
=find()에서 찾아온 값을 skip을 통해 제외하고 나머지를 검색
몽고DB에서 shell은 몽고DB 명령어뿐만 아니라 자바스크립트 코드도 실행 가능
-자바스크립트
var showpage = function(page=3){
return db.orders.find().sort({“id”: -1 }).skip((page-1)*2).limit(2); }
-함수 실행
showPage(3)
id 번호 1234를 제외한 5,6번이 나옴


update : set, unset , upsert , 조건 변경

set
=컬럼 값 변경

unset
=컬럼 값을 제거 {score : 1} 1은 true의 의미

upsert
=조건이 없으면 컬럼을 새로 추가 또는 조건이 있으면 컬럼내용 변경

age 20 보다 작거나 같은 조건에 score를 변경
{age:{lte:20}} = 조건 절


mongoDB와 자바연동
http://mongodb.github.io/mongo-java-driver/





1)Eclipse에 mongo. jar 라이브러리 추가

2)

3)



댓글

이 블로그의 인기 게시물

[DATA STRUCTURE] 큐 QUEUE

큐[QUEUE]의개념 Queue is an abstract data structure, somewhat similar to Stacks.  Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue).  Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first. First In First Out Basic Operation Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues − enqueue() − add (store) an item to the queue. dequeue() − remove (access) an item from the queue. Few more functions are required to make the above-mentioned queue operation efficient. These are − peek() − Gets the element at the front of the queue without removing it. isfull() − Checks if the queue is full.[Boolean] isempty() − Checks if the queue is empty.[Boolean] 큐의사용 예 너비 우선 탐색(BFS, Breadth-First Search) 구현 처리해야 할 노...

자바FX 설치하기(펌)

필요한것 최신 Java JDK 8 (JavaFX 8 포함) Eclipse 4.6 또는 e(fx)clipse 플러그인을 포함한 그 이상 버전. 가장 쉬운 방법은 e(fx)clipse website 에서 미리 포함되어 있는 버전을 받습니다. 다른 대안으로는 Eclipse에서 update site 를 이용할 수 있습니다. Gluon이 제공하는 Scene Builder 8.0 (왜냐하면 Oracle은 오직 소스 코드 형태로만 제공 하기 때문입니다.) Eclipse 설정 Eclipse가 JDK 8과 Scene Builder를 사용할 수 있게 해야 합니다: Eclipse Preferences를 열어 *Java | Installed JREs*를 찾습니다. *Add..*를 클릭한 후 *Standard VM*를 선택, JDK 8이 설치되어 있는 *디렉토리*를 고릅니다. 다른 JRE나 JDK를 삭제합니다. 그러면 JDK 8 하나만 남습니다. *Java | Compiler*를 찾아 Compiler compliance level을 1.8로 설정합니다. JavaFX  부분을 찾아 Scene Builder 실행 파일이 있는 경로를 지정합니다. JavaFX 프로젝트 만들기 e(fx)clipse를 설치하고 나서 Eclipse에서  File | New | Other… , *JavaFX Project*를 고릅니다. 프로젝트 이름(예:  AddressApp )을 작성한 다음, *Finish*를 클릭합니다. 자동으로 생성되는  application  패키지가 있다면 삭제하세요. 패키지 만들기 시작부터 우리는 좋은 소프트웨어 디자인 원칙을 따를 겁니다. 가장 중요한 원칙이  모델-뷰-컨트롤러 입니다. 이에 따라 우리의 코드를 세 단위로 나누고 각 패키지를 만듭니다 (src 디렉토리에서 마우스 오른쪽 클릭 후  New… | Package  선택): ...