본문 바로가기
my_lesson/_NodeJSmongoDB

Mongodb

by boolean 2017. 8. 26.
728x90

MongoDB

Install Mongodb





Start Mongodb : sudo service mongod start

mongodb change field's name modify field's name rename

> db.company.updateMany({}, {$rename:{"address":"m_email"}})

{ "acknowledged" : true, "matchedCount" : 18, "modifiedCount" : 17 }


mongodb change value of field, modify value of field, set

> db.company.update({m_name:"John"}, {$set: {m_email:"khj@khj.com"}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


mongoexport - JSON

CAUTION : mongoexport must be run directly from the system command line.

If the database is testDB and the collection is testCol
>use testDB
>db.testCol..insert( { _id: 1, volume: NumberLong('2980000'), date: new Date() } )

: mongoexport --db testDB --collection testCol --out testCol.json


mongoimport - JSON

CAUTION : mongoexport must be run directly from the system command line.

mongoimport restores a database from a backup taken with mongoexport.
If the database is testDB and the collection is testCol

: mongoimport --db testDB --collection testCol --file testCol.json


mongoexport - CSV

CAUTION : mongoexport must be run directly from the system command line.

When you export in CSV format, you must specify the fields in the documents to export. The operation specifies the name and address fields to export.
If the database is testDB and the collection is testCol and the field1 is name and field2 is address

: mongoexport --db testDB --collection testCol --type=csv --fields name,address --out /opt/backups/testCol.csv


mongoimport - CSV

CAUTION : mongoexport must be run directly from the system command line.

When you import in CSV format, you must specify the fields in the documents to import. The operation specifies the name and address fields to export.
If the database is testDB and the collection is testCol 

: mongoimport --db testDB --collection testCol --type csv --headerline --file /opt/backups/testCol.csv



mongodb - SHARDING

shard 1

~$ sudo mkdir -p /data/shard0/rs0 /data/shard0/rs1 /data/shard0/rs2

~$ sudo mongod --replSet s0 --logpath "s0-r0.log" --dbpath /data/shard0/rs0 --port 37017 --fork --shardsvr --smallfiles

~$ sudo mongod --replSet s0 --logpath "s0-r1.log" --dbpath /data/shard0/rs1 --port 37018 --fork --shardsvr --smallfiles

~$ sudo mongod --replSet s0 --logpath "s0-r2.log" --dbpath /data/shard0/rs2 --port 37019 --fork --shardsvr --smallfiles

~$ mongo --port 37017

> config = { _id: "s0", members:[

{ _id : 0, host : "localhost:37017"},

{ _id : 1, host : "localhost:37018"},

{ _id : 2, host : "localhost:37019"}]};

> rs.initiate(config)

shard 2

~$ sudo mkdir -p /data/shard1/rs0 /data/shard1/rs1 /data/shard1/rs2

~$ sudo mongod --replSet s1 --logpath "s1-r0.log" --dbpath /data/shard1/rs0 --port 47017 --fork --shardsvr --smallfiles

~$ sudo mongod --replSet s1 --logpath "s1-r1.log" --dbpath /data/shard1/rs1 --port 47018 --fork --shardsvr --smallfiles

~$ sudo mongod --replSet s1 --logpath "s1-r2.log" --dbpath /data/shard1/rs2 --port 47019 --fork --shardsvr --smallfiles

~$ mongo --port 47017

> config = { _id: "s1", members:[

{ _id : 0, host : "localhost:47017"},

{ _id : 1, host : "localhost:47018"},

{ _id : 2, host : "localhost:47019"}]};

> rs.initiate(config)

shard 3

~$ sudo mkdir -p /data/shard2/rs0 /data/shard2/rs1 /data/shard2/rs2

~$ sudo mongod --replSet s2 --logpath "s2-r0.log" --dbpath /data/shard2/rs0 --port 57017 --fork --shardsvr --smallfiles

~$ sudo mongod --replSet s2 --logpath "s2-r1.log" --dbpath /data/shard2/rs1 --port 57018 --fork --shardsvr --smallfiles

~$ sudo mongod --replSet s2 --logpath "s2-r2.log" --dbpath /data/shard2/rs2 --port 57019 --fork --shardsvr --smallfiles

~$ mongo --port 37017

> config = { _id: "s2", members:[

{ _id : 0, host : "localhost:57017"},

{ _id : 1, host : "localhost:57018"},

{ _id : 2, host : "localhost:57019"}]};

> rs.initiate(config)


config

FailedToParse: mirrored config server connections are not supported

일때 아래와 같이 해보자

~$ sudo mkdir -p /data/config/congig-a /data/config/config-b /data/config/config-c

~$ sudo mongod --replSet conf --logpath "cfg-a.log" --dbpath /data/config/config-a --port 57040 --fork --configsvr --smallfiles

~$ sudo mongod --replSet conf --logpath "cfg-b.log" --dbpath /data/config/config-b --port 57041 --fork --configsvr --smallfiles

~$ sudo mongod --replSet conf --logpath "cfg-c.log" --dbpath /data/config/config-c --port 57042 --fork --configsvr --smallfiles

~$ mongo --port 57040

> config = { _id: "conf", members:[

{ _id : 0, host : "localhost:57040"},

{ _id : 1, host : "localhost:57041"},

{ _id : 2, host : "localhost:57042"}]};

> rs.initiate(config)

~$ sudo mongos --logpath "mongos-1.log" --configdb "conf/localhost:57040,localhost:57041,localhost:57042" --fork --port 57200 


addshard

~$ mongo --port 57200

> db.adminCommand( { addshard : "s0/"+"localhost:37017"} );

> db.adminCommand( { addshard : "s1/"+"localhost:47017"} );

> db.adminCommand( { addshard : "s2/"+"localhost:57017"} );

> db.adminCommand({enableSharding: "school})

> db.adminCommand({shardCollection: "schoool.students", key: {student_id:1}});



printShardingStatus(); - 현재 샤드 상태 조회

removeshard - 해당 데이터 베이스 제거



댓글