2011년 5월 20일 금요일

MongoDB

MongoDB

  • 개요
  • 문서 위주의 데이터 베이스
  • NoSQL (cf. MySQL RDB), schema-less
  • 공식 사이트 MongoDB.org
  • 필요 지식
    • 유닉스 명령어 : Unix CLI
    • 자바스크립트 : JavaScript
    • 정규표현식 : Regular Expressing
    • 로컬개발 환경 Vagrant
    • 사용 버전
      mongo —version
      MongoDB shell version: 2.6.0

용어 정리

  • 데이터 베이스 아래, 콜렉션(Collection)이 있고, 그 아래 문서(Document)가 있다.
MySQLMongoDB
Databasedatabase
TableCollection
RecordDocuement
  • Document는 JavaScript로 처리하며 key:val로 구성된다.
  • schema-less 이므로 기존 RBD처럼 Docuemnt가 고정되지 않는다.
  • 예를 들면
    • hephaex는 name, score 의 키값을 가지지만
    • foo는 name, score,team을 가진다.
  • Database : BlogApp
    • Collection : Posts
      • Document
      • Document
      • Document
    • Collection : Users
      • Document
         {key:val, key:val, ... , key:val}
        
      • Document
         {name:"hephaex", score: 100, ... , key:val}
        
      • Document
         {name:"foo", score: 90, team:3, ..., key:val }
        

Database

  • show dbs; 데이터 베이스 리스트
  • use _ ; 신규작성, 전환
  • db.drobDatabase() ; 제거
    mongo mydb
    MongoDB shell version: 2.6.0
    connecting to: mydb
  • exit : 빠져나가기
  • help : 도움말
  • show dbs; // 데이터 베이스 보기
    admin (empty)
    local 0.078GB
  • 콜렉션을 생성해 보자.
    db.createCollection(‘test’);
    { “ok” : 1 }
  • 생성된 콜렉션을 확인해 보자
    show dbs;
    admin (empty)
    local 0.078GB
    mydb 0.078GB
  • 데이터 베이스를 바꿔보자.
    use mydb2
    switched to db mydb2
  • 주의할 점은 mydb2를 콜렉션으로 만들지 않았기 때문에 show dbs하면 mydb만 나온다.
    즉 생성되지 않은 것이라서 내용이 바뀌지 않는다.
  • 데이터 베이스를 지워보자.
use mydb
switched to db mydb
db.dropDatabase();
{ “dropped” : “mydb”, “ok” : 1 }
show dbs;
admin (empty)
local 0.078GB
  • mydb가 삭제된 것을 학인할 수 있다.

콜렉션을 다뤄보자.

  • db.createCollection(‘col’); 새로 만들기
  • show collections; 콜렉션 목록 보기
  • db._.renameCollection(‘newCol’); 이름 바꾸기
  • db._.drop(); 콜렉션 지우기

example)

  • 우선 mydb로 바꾸고 데이터 베이스가 있는지 확인해 보자.
use mydb;
switched to db mydb
show dbs;
admin (empty)
local 0.078GB
  • 콜렉션 users를 생성해 보자.
db.createCollection(‘users’);
{ “ok” : 1 }
  • 생성된 users 콜렉션을 확인해 보자.
show collections;
system.indexes
users
  • users 콜렉션을 person으로 바꿔보자.
db.users.renameCollection(‘person’);
{ “ok” : 1 }
show collections
person
system.indexes
  • 콜렉션 person을 지워보자.
db.person.drop();
true
show collections;
system.indexes

도규먼트를 다뤄보자.

  • db.COL.insert(); 새로 만들기
  • db.COL.find(); 전부 검색하기
  • db.COL.remove(); 지우기
  • db.COL.count(); 갯수

example)

  • 데이터 베이스를 확인해 보자.
show dbs;
admin (empty)
local 0.078GB
mydb 0.078GB
  • mydb 데이터 베이스를 선택하고 콜렉션을 확인하자.
use mydb;
switched to db mydb
show collections
system.indexes
  • users라는 콜렉션을 생성하고 데이터를 넣자.
db.createCollection(‘users’);
{ “ok” : 1 }
db.users.insert({name:”hephaex”, score:100});
WriteResult({ “nInserted” : 1 })
db.users.insert({name:”foo”, tags:[“web”,”mobile”]});
WriteResult({ “nInserted” : 1 })
  • 여기서 foo는 NoSQL이기 때문에 스키마를 배열로 넣을 수 있다. (*)
  • users 콜렉션에 어떤것이 들어 있는지 확인해 보자.
db.users.find();
{ “_id” : ObjectId(“537ab7e83cba50cc281a7e92”), “name” : “hephaex”, “score” : 100 }
{ “_id” : ObjectId(“537ab8243cba50cc281a7e93”), “name” : “foo”, “tags” : [ “web”, “mobile” ] }
  • 도큐먼트 수를 헤아려 보자.
db.users.count();
2
  • 도큐먼트를 삭제해 보자.
db.users.find();
{ “_id” : ObjectId(“537ab7e83cba50cc281a7e92”), “name” : “hephaex”, “score” : 100 }
{ “_id” : ObjectId(“537ab8243cba50cc281a7e93”), “name” : “foo”, “tags” : [ “web”, “mobile” ] }
삭제되지 않는것에 주의할 것.
db.users.remove();
2014-05-20T11:21:46.681+0900 remove needs a query at src/mongo/shell/collection.js:299
지우려면 remove({})를 사용해야 document가 지워진다.
db.users.remove({});
WriteResult({ “nRemoved” : 2 })

자바스크립트로 데이터를 넣어보자.

  • MongoDB는 자바스크립트를 바로 쓸수 있다.
    for (var i = 0; i < 4; i++) {
    db.users.insert({
      name : "user-" + i,
      team : "team-" + (i % 3),
      score : Math.floor(Math.random() * 100)
      });
    }
    
    db.users.find()
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e94”), “name” : “user-0”, “team” : “team-0”, “score” : 51 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e95”), “name” : “user-1”, “team” : “team-1”, “score” : 48 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e96”), “name” : “user-2”, “team” : “team-2”, “score” : 91 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “name” : “user-3”, “team” : “team-0”, “score” : 43 }
for (var i = 4; i < 6; i++) {
  db.users.insert({
    name : "user-" + i,
    team : "team-" + (i % 3)
    });
}
결과 4건은 score가 있고 2건은 score가 없다.
{ “_id” : ObjectId(“537ac2fc3cba50cc281a7e94”), “name” : “user-0”, “team” : “team-0”, “score” : 51 }
{ “_id” : ObjectId(“537ac2fc3cba50cc281a7e95”), “name” : “user-1”, “team” : “team-1”, “score” : 48 }
{ “_id” : ObjectId(“537ac2fc3cba50cc281a7e96”), “name” : “user-2”, “team” : “team-2”, “score” : 91 }
{ “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “name” : “user-3”, “team” : “team-0”, “score” : 43 }
{ “_id” : ObjectId(“537ac37f3cba50cc281a7e98”), “name” : “user-4”, “team” : “team-1” }
{ “_id” : ObjectId(“537ac37f3cba50cc281a7e99”), “name” : “user-5”, “team” : “team-2” }

find() 에 조건을 넣어보자.

  • db.users.find(); 전체를 출력한다.
  • db.users.find({key:value}); key와 value에 해당하는 값이 출력된다.

ex)

  • name이 user-1인것을 출력
    db.users.find({name: “user-1”});
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e95”), “name” : “user-1”, “team” : “team-1”, “score” : 48 }
  • 정규표현식을 사용해서 user-1~3을 출력
    db.users.find({name: /user-[1-3]/});
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e95”), “name” : “user-1”, “team” : “team-1”, “score” : 48 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e96”), “name” : “user-2”, “team” : “team-2”, “score” : 91 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “name” : “user-3”, “team” : “team-0”, “score” : 43 }
  • score가 30 이상인것을 출력.
    • $gt $gte
    • $lt $lte
    • $ne
      db.users.find({score: {$gt:30} });
      { “_id” : ObjectId(“537ac2fc3cba50cc281a7e94”), “name” : “user-0”, “team” : “team-0”, “score” : 51 }
      { “_id” : ObjectId(“537ac2fc3cba50cc281a7e95”), “name” : “user-1”, “team” : “team-1”, “score” : 48 }
      { “_id” : ObjectId(“537ac2fc3cba50cc281a7e96”), “name” : “user-2”, “team” : “team-2”, “score” : 91 }
      { “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “name” : “user-3”, “team” : “team-0”, “score” : 43 }
  • user-1이 아닌것을 출력
    db.users.find({name : { $ne: “user-1”} } );
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e94”), “name” : “user-0”, “team” : “team-0”, “score” : 51 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e96”), “name” : “user-2”, “team” : “team-2”, “score” : 91 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “name” : “user-3”, “team” : “team-0”, “score” : 43 }
    { “_id” : ObjectId(“537ac37f3cba50cc281a7e98”), “name” : “user-4”, “team” : “team-1” }
    { “_id” : ObjectId(“537ac37f3cba50cc281a7e99”), “name” : “user-5”, “team” : “team-2” }

$and, $or, distinct 를 사용해 보자.

  • AND
    db.users.find({
    team: "team-0",
    score: { $lt: 50}
    });
    
    … … … { “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “name” : “user-3”, “team” : “team-0”, “score” : 43 }
db.users.find({
  $and : [
    { team: "team-0" } ,
    { score: { $lt: 50} }
    ]
});
… … … … … { “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “name” : “user-3”, “team” : “team-0”, “score” : 43 }
  • $OR
    • $in : [ xxx , xxx ] 가 배열에 있는지 검사한다.
    • $exists: 있는 것에만 검사한다.
      db.users.find({
      $or : [
      { team: { $in: ["team-0", "team-1"] } } ,
      { score: { $exists : true } }
      ]
      });
      
      … … … … …
      { “_id” : ObjectId(“537ac2fc3cba50cc281a7e94”), “name” : “user-0”, “team” : “team-0”, “score” : 51 }
      { “_id” : ObjectId(“537ac2fc3cba50cc281a7e95”), “name” : “user-1”, “team” : “team-1”, “score” : 48 }
      { “_id” : ObjectId(“537ac2fc3cba50cc281a7e96”), “name” : “user-2”, “team” : “team-2”, “score” : 91 }
      { “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “name” : “user-3”, “team” : “team-0”, “score” : 43 }
      { “_id” : ObjectId(“537ac37f3cba50cc281a7e98”), “name” : “user-4”, “team” : “team-1” }
  • distinct
    • 어떤 종류가 있는지 검사
      db.users.distinct(‘team’);
      [ “team-0”, “team-1”, “team-2” ]

추출 데이터 결과를 가공해 보자.

  • db.users.find({}) 을 가공해보자.
  • find( {}, 조건식); 을 사용한다.
  • name만 선택하면
    db.users.find({},{name:1});
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e94”), “name” : “user-0” }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e95”), “name” : “user-1” }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e96”), “name” : “user-2” }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “name” : “user-3” }
    { “_id” : ObjectId(“537ac37f3cba50cc281a7e98”), “name” : “user-4” }
    { “_id” : ObjectId(“537ac37f3cba50cc281a7e99”), “name” : “user-5” }
  • name만 선택하지 않으려면
    db.users.find({},{name:0});
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e94”), “team” : “team-0”, “score” : 51 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e95”), “team” : “team-1”, “score” : 48 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e96”), “team” : “team-2”, “score” : 91 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “team” : “team-0”, “score” : 43 }
    { “_id” : ObjectId(“537ac37f3cba50cc281a7e98”), “team” : “team-1” }
    { “_id” : ObjectId(“537ac37f3cba50cc281a7e99”), “team” : “team-2” }
  • name과 score만 출력해 보자.
    db.users.find({}, {name:1, score:1});
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e94”), “name” : “user-0”, “score” : 51 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e95”), “name” : “user-1”, “score” : 48 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e96”), “name” : “user-2”, “score” : 91 }
    { “_id” : ObjectId(“537ac2fc3cba50cc281a7e97”), “name” : “user-3”, “score” : 43 }
    { “_id” : ObjectId(“537ac37f3cba50cc281a7e98”), “name” : “user-4” }
    { “_id” : ObjectId(“537ac37f3cba50cc281a7e99”), “name” : “user-5” }
  • 두가지를 mix할때 제외가 되면 에러가 발생한다.
    db.users.find( {}, {name:1, score:0});
    error: {
    “$err” : “Can’t canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion.”,
    “code” : 17287
    }
  • 혼용 mix해서 사용하는 것은 _id만 가능하다는 점에 주의할 것.
    db.users.find( {}, {name:1, score:1, _id:0});
    { “name” : “user-0”, “score” : 51 }
    { “name” : “user-1”, “score” : 48 }
    { “name” : “user-2”, “score” : 91 }
    { “name” : “user-3”, “score” : 43 }
    { “name” : “user-4” }
    { “name” : “user-5” }
  • findOne () 출력 수를 제한해 보자.
    db.users.findOne({}, {_id:0});
    { “name” : “user-0”, “team” : “team-0”, “score” : 51 }
  • limit(3) 출력수를 3개로 제한해 보자.
    db.users.find( {}, {_id:0}).limit(3);
    { “name” : “user-0”, “team” : “team-0”, “score” : 51 }
    { “name” : “user-1”, “team” : “team-1”, “score” : 48 }
    { “name” : “user-2”, “team” : “team-2”, “score” : 91 }
  • skip(2) 마지막 2개를 건너 뛰어 보자.
    db.users.find( {}, {_id:0}).skip(2).limit(3);
    { “name” : “user-2”, “team” : “team-2”, “score” : 91 }
    { “name” : “user-3”, “team” : “team-0”, “score” : 43 }
    { “name” : “user-4”, “team” : “team-1” }
  • sort(score:1) 정령해 보자. (1 오름차순, -1 내림차순)
    db.users.find( {}, {_id:0}).sort( {score:-1});
    { “name” : “user-2”, “team” : “team-2”, “score” : 91 }
    { “name” : “user-0”, “team” : “team-0”, “score” : 51 }
    { “name” : “user-1”, “team” : “team-1”, “score” : 48 }
    { “name” : “user-3”, “team” : “team-0”, “score” : 43 }
    { “name” : “user-4”, “team” : “team-1” }
    { “name” : “user-5”, “team” : “team-2” }
db.users.find( {}, {_id:0}).sort({score:1});
{ “name” : “user-4”, “team” : “team-1” }
{ “name” : “user-5”, “team” : “team-2” }
{ “name” : “user-3”, “team” : “team-0”, “score” : 43 }
{ “name” : “user-1”, “team” : “team-1”, “score” : 48 }
{ “name” : “user-0”, “team” : “team-0”, “score” : 51 }
{ “name” : “user-2”, “team” : “team-2”, “score” : 91 }

색인(index)를 사용해 보자.

  • 키를 사용하면 검색 속도를 빠르게 만든다.
  • db.COL.getIndexes(); 인덱스 출력
    db.users.getIndexes()
    [
    {
       "v" : 1,
       "key" : {
           "_id" : 1
       },
       "name" : "_id_",
       "ns" : "mydb.users"
    
    }
    ]
  • db.COL.ensureIndex(); score를 오름차순으로 하는 인덱스 추가
    db.users.ensureIndex({score:1});
    {
    “createdCollectionAutomatically” : false,
    “numIndexesBefore” : 1,
    “numIndexesAfter” : 2,
    “ok” : 1
    }
  • 맞게 색인이 되었는지 확인해 보자.
    db.users.getIndexes();
    [
    {
       "v" : 1,
       "key" : {
           "_id" : 1
       },
       "name" : "_id_",
       "ns" : "mydb.users"
    
    },
    {
       "v" : 1,
       "key" : {
           "score" : 1
       },
       "name" : "score_1",
       "ns" : "mydb.users"
    
    }
    ]
  • unique key를 설정해 보자.
    db.users.ensureIndex( {name:1}, {unique:true});
    {
    “createdCollectionAutomatically” : false,
    “numIndexesBefore” : 1,
    “numIndexesAfter” : 2,
    “ok” : 1
    }
  • 유일한 값에 추가로 값을 넣으려 하면 에러가 발생한다.
    db.users.insert( {name:”user-0”});
    WriteResult({
    “nInserted” : 0,
    “writeError” : {
       "code" : 11000,
       "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.users.$name_1  dup key: { : \"user-0\" }"
    
    }
    })
  • db.COL.dropIndex(); 인덱스 삭제
    db.users.dropIndex( {score:1});
    { “nIndexesWas” : 2, “ok” : 1 }

update 갱신을 해보자.

  • update를 사용해서 쓰면 될것이라고 생각하지만 score 만 들어간다.
  • 즉 이것은 스키마레스이기 때문에 발생하는 현상이다. 주의하자.
db.users.update( {name:”user-0”}, {score:100});
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.users.find({}, {_id:0});
{ “score” : 100 }
{ “name” : “user-1”, “team” : “team-1”, “score” : 48 }
{ “name” : “user-2”, “team” : “team-2”, “score” : 91 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 43 }
{ “name” : “user-4”, “team” : “team-1” }
{ “name” : “user-5”, “team” : “team-2” }
  • user-0에 score만 바꿔 주려면 $set을 사용하면 된다.
db.users.update( {name:”user-1”}, {$set: {score:100}});
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.users.find( {}, {_id:0});
{ “score” : 100 }
{ “name” : “user-1”, “team” : “team-1”, “score” : 100 }
{ “name” : “user-2”, “team” : “team-2”, “score” : 91 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 43 }
{ “name” : “user-4”, “team” : “team-1” }
{ “name” : “user-5”, “team” : “team-2” }
  • $unset 지울때 사용하는데 이때 “”하는 것에 주의하자.
db.users.update( {name:”user-1”},{$unset:{score:””}});
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.users.find( {}, {_id:0});
{ “score” : 100 }
{ “name” : “user-1”, “team” : “team-1” }
{ “name” : “user-2”, “team” : “team-2”, “score” : 91 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 43 }
{ “name” : “user-4”, “team” : “team-1” }
{ “name” : “user-5”, “team” : “team-2” }
  • $inc 값을 줄이거나 늘려준다.
db.users.update( {name:”user-2”}, {$inc: {score:-10}});
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.users.find( {}, {_id:0});
{ “score” : 100 }
{ “name” : “user-1”, “team” : “team-1” }
{ “name” : “user-2”, “team” : “team-2”, “score” : 81 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 43 }
{ “name” : “user-4”, “team” : “team-1” }
{ “name” : “user-5”, “team” : “team-2” }
  • $rename 이름을 바꿀때 사용한다.
db.users.update({name:”user-2”}, {$rename:{score:”myscore”}});
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.users.find( {}, {_id:0});
{ “score” : 100 }
{ “name” : “user-1”, “team” : “team-1” }
{ “name” : “user-2”, “team” : “team-2”, “myscore” : 81 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 43 }
{ “name” : “user-4”, “team” : “team-1” }
{ “name” : “user-5”, “team” : “team-2” }

multi 를 사용해 보자.

  • update 하면 한개만 증가하므로 주의하자. 이때 사용하는 것이 multi이다.
    db.users.find ({},{_id:0});
    { “name” : “user-0”, “team” : “team-0”, “score” : 87 }
    { “name” : “user-1”, “team” : “team-1”, “score” : 6 }
    { “name” : “user-2”, “team” : “team-2”, “score” : 10 }
    { “name” : “user-3”, “team” : “team-0”, “score” : 91 }
    { “name” : “user-4”, “team” : “team-1”, “score” : 37 }
    { “name” : “user-5”, “team” : “team-2”, “score” : 3 }
db.users.update( {team:”team-0”},{$inc:{score:10}});
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
db.users.find( {},{_id:0});
{ “name” : “user-0”, “team” : “team-0”, “score” : 97 }
{ “name” : “user-1”, “team” : “team-1”, “score” : 6 }
{ “name” : “user-2”, “team” : “team-2”, “score” : 10 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 91 }
{ “name” : “user-4”, “team” : “team-1”, “score” : 37 }
{ “name” : “user-5”, “team” : “team-2”, “score” : 3 }
  • team-0에 해당하는 모든 것을 10식 증가하려면 multi를 사용한다.
db.users.update( {team:”team-0”}, {$inc:{score:10}},{multi:true});
WriteResult({ “nMatched” : 2, “nUpserted” : 0, “nModified” : 2 })
db.users.find( {}, {_id:0});
{ “name” : “user-0”, “team” : “team-0”, “score” : 107 }
{ “name” : “user-1”, “team” : “team-1”, “score” : 6 }
{ “name” : “user-2”, “team” : “team-2”, “score” : 10 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 101 }
{ “name” : “user-4”, “team” : “team-1”, “score” : 37 }
{ “name” : “user-5”, “team” : “team-2”, “score” : 3 }

upsert, remove

  • upsert : (update / insert)
db.users.update({name:”hephaex”}, {name:”hephaex”, team:”team-0”,score:100});
WriteResult({ “nMatched” : 0, “nUpserted” : 0, “nModified” : 0 })
db.users.find( {}, {_id:0});
{ “name” : “user-0”, “team” : “team-0”, “score” : 107 }
{ “name” : “user-1”, “team” : “team-1”, “score” : 6 }
{ “name” : “user-2”, “team” : “team-2”, “score” : 10 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 101 }
{ “name” : “user-4”, “team” : “team-1”, “score” : 37 }
{ “name” : “user-5”, “team” : “team-2”, “score” : 3 }
  • name에 hephaex가 없기 때문에 자료는 변하되지 않았다.
  • 여기서 upsert:true를 추가하면 update와 동시에 insert를 한다.
db.users.update({name:”hephaex”}, {name:”hephaex”, team:”team-0”,score:100},{upsert:true});
WriteResult({
“nMatched” : 0,
“nUpserted” : 1,
“nModified” : 0,
“_id” : ObjectId(“537b07559b3de117452cd708”)
})
확인해 보자
db.users.find( {}, {_id:0});
{ “name” : “user-0”, “team” : “team-0”, “score” : 107 }
{ “name” : “user-1”, “team” : “team-1”, “score” : 6 }
{ “name” : “user-2”, “team” : “team-2”, “score” : 10 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 101 }
{ “name” : “user-4”, “team” : “team-1”, “score” : 37 }
{ “name” : “user-5”, “team” : “team-2”, “score” : 3 }
{ “name” : “hephaex”, “team” : “team-0”, “score” : 100 }
  • remove 에 조건에 맞는 것만 지우기
db.users.remove( {name:”hephaex”});
WriteResult({ “nRemoved” : 1 })
db.users.find( {}, {_id:0});
{ “name” : “user-0”, “team” : “team-0”, “score” : 107 }
{ “name” : “user-1”, “team” : “team-1”, “score” : 6 }
{ “name” : “user-2”, “team” : “team-2”, “score” : 10 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 101 }
{ “name” : “user-4”, “team” : “team-1”, “score” : 37 }
{ “name” : “user-5”, “team” : “team-2”, “score” : 3 }

mongodump, mongorestore를 해보자.

  • 백업과 복원과정이다.
mongodump -d mydb
connected to: 127.0.0.1
2011-05-20T16:48:26.768+0900 DATABASE: mydb to dump/mydb
2011-05-20T16:48:26.769+0900 mydb.system.indexes to dump/mydb/system.indexes.bson
2011-05-20T16:48:26.770+0900 1 documents
2011-05-20T16:48:26.770+0900 mydb.users to dump/mydb/users.bson
2011-05-20T16:48:26.771+0900 6 documents
2011-05-20T16:48:26.771+0900 Metadata for mydb.users to dump/mydb/users.metadata.json
  • 폴더에서 확인해 보면 dump가 생겼다.
ls
dump
  • 그럼 복원을 해보자.
  • 우선 자료를 변형 시키자. (team-0를 모두 지우자.)
db.users.remove({team:”team-0”});
WriteResult({ “nRemoved” : 2 })
db.users.find( {},{_id:0});
{ “name” : “user-1”, “team” : “team-1”, “score” : 6 }
{ “name” : “user-2”, “team” : “team-2”, “score” : 10 }
{ “name” : “user-4”, “team” : “team-1”, “score” : 37 }
{ “name” : “user-5”, “team” : “team-2”, “score” : 3 }
  • 복원해보자.
mongorestore —drop
connected to: 127.0.0.1
2011-05-20T16:54:27.841+0900 dump/mydb/users.bson
2011-05-20T16:54:27.841+0900 going into namespace [mydb.users]
2011-05-20T16:54:27.841+0900 dropping
6 objects found
2011-05-20T16:54:27.844+0900 Creating index: { key: { id: 1 }, name: “_id“, ns: “mydb.users” }
mongo mydb
MongoDB shell version: 2.6.0
connecting to: mydb
db.users.find( {}, {_id:0});
{ “name” : “user-0”, “team” : “team-0”, “score” : 107 }
{ “name” : “user-1”, “team” : “team-1”, “score” : 6 }
{ “name” : “user-2”, “team” : “team-2”, “score” : 10 }
{ “name” : “user-3”, “team” : “team-0”, “score” : 101 }
{ “name” : “user-4”, “team” : “team-1”, “score” : 37 }
{ “name” : “user-5”, “team” : “team-2”, “score” : 3 }
  • 복원된 것을 확인할 수 있다.

댓글 없음:

댓글 쓰기