티스토리 뷰

Programming/MongoDB

mongodb install & basic commands

Mr. Curious 2018. 1. 25. 00:35

설치 

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

설정 

http://ngee.tistory.com/372


Shell

$ mongo

> show dbs
admin  0.000GB
local  0.000GB

> use school_db
switched to db school_db

> db
school_db

> db.createUser({user: "principal", pwd: "123", roles: ["readWrite", "dbAdmin"]});
Successfully added user: { "user" : "principal", "roles" : [ "readWrite", "dbAdmin" ] }

> db.createCollection('students');
{ "ok" : 1 }

> show collections
students

> db.students.insert({name: 'Mike', age: 23, grade: "A"});
WriteResult({ "nInserted" : 1 })

> db.students.find();
{ "_id" : ObjectId("5a68aa36cc35afef83998952"), "name" : "Mike", "age" : 23, "grade" : "A" }

> db.students.insert([{name: 'Michael', age: 41, grade: "B+"},
... {name: 'Victor', age: 33, grade: "A+"},
... {name: 'Josh', age: 28, grade: "C+"}]);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

> db.students.find();
{ "_id" : ObjectId("5a68aa36cc35afef83998952"), "name" : "Mike", "age" : 23, "grade" : "A" }
{ "_id" : ObjectId("5a68aaf9cc35afef83998953"), "name" : "Michael", "age" : 41, "grade" : "B+" }
{ "_id" : ObjectId("5a68aaf9cc35afef83998954"), "name" : "Victor", "age" : 33, "grade" : "A+" }
{ "_id" : ObjectId("5a68aaf9cc35afef83998955"), "name" : "Josh", "age" : 28, "grade" : "C+" }

> db.students.find().pretty();
{
"_id" : ObjectId("5a68aa36cc35afef83998952"),
"name" : "Mike",
"age" : 23,
"grade" : "A"
}
{
"_id" : ObjectId("5a68aaf9cc35afef83998953"),
"name" : "Michael",
"age" : 41,
"grade" : "B+"
}
{
"_id" : ObjectId("5a68aaf9cc35afef83998954"),
"name" : "Victor",
"age" : 33,
"grade" : "A+"
}
{
"_id" : ObjectId("5a68aaf9cc35afef83998955"),
"name" : "Josh",
"age" : 28,
"grade" : "C+"
}

> db.students.remove({});
WriteResult({ "nRemoved" : 4 })

> db.students.find().pretty();

> db.dropDatabase()
{ "dropped" : "school_db", "ok" : 1 }


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함