03 August 2015

Data Path

/data/db

Shell Start

$ mongo

Database

Create

db를 새로 만드는 커맨드는 없다. 대신 새로운 db name 을 use한 다음에 collection을 만들고 document 를한 개 이상 생성하면 db가 만들어진다.

ETC

  • db : 현재 db 확인
  • show dbs : db 목록
  • use db이름 : db 사용
  • db.dropDatabase() : db 삭제

Collection

Create

db.createCollection(<name>, { capped: <boolean>,
                              autoIndexId: <boolean>,
                              size: <number>,
                              max: <number>,
                              storageEngine: <document> } )

ETC

  • show collections
  • db.collection
  • db["collection"]
  • db.getCollection("collection")
  • db.collection.drop()

Document

create

db.collection.insert({
    name: { first: 'Dennis', last: 'Ritchie'},
    birth: new Date('Sep 09, 1941'),
    death: new Date('Oct 12, 2011'),
    contribs: [ 'UNIX', 'C' ]
});

read

  • db.collection.find({name:'joe'}) : joe 찾기
  • db.collection.find({name:'joe'},{age :true}) : joe의 나이
  • db.collection.find({}).sort({age:1}) : 전체를 나이순으로 정렬
  • db.collection.find({}).limit(10).skip(10) : 11번째부터 20번째까지
  • db.collection.count() : 갯수
  • db.collection.findOne({name:'joe'}) : joe 하나만 찾기

update

  • db.collection.update({name:'joe'}, {age: 30}) : joe 한명의 나이를 30으로 업데이트
  • db.collection.update({name:'joe'}, {age: 30}, true) : joe 한명의 나이를 30으로 업데이트 하는데 만약에 없으면 30살의 joe를 새로 insert.
  • db.collection.update({name:'joe'}, {age: 30}, false, true) : 모든 joe 의 나이를 30으로 업데이트.
  • db.collection.save({_id:100, name:'joe', age: 30}) 해당 아이디를 업데이트
  • db.collection.save({name:'joe', age: 30}) 아이디가 없으면 insert

delete

  • db.collection.remove( { age: { $gt: 20 } } ) : 20살이 넘으면 삭제
  • db.collection.remove( { age: 20 }, 1 ) : 20살짜리중 하나만 삭제
  • db.collection.remove( { age: 20, $isolated: 1 }) : isolating


blog comments powered by Disqus
처음으로