기본 instance 구성

 server아래에 여러 database가 있고 , database 마다 collection이 있는데 collection은 RDB에서 Table 로 이해하면 된다. 

 

 

접속방법 : mongo

# mongo
MongoDB shell version v5.0.26
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c7784ca1-105e-4a8e-8448-a1a5e0169f24") }
MongoDB server version: 5.0.26
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removedin
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting:
        2024-04-25T08:28:59.502+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2024-04-25T08:28:59.503+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never' in this binary version
        2024-04-25T08:28:59.503+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never' in this binary version
---
>

 

- root로 들어갔다 

 

database 명령어

> show database

> use [database name]  <- 없으면 생성 후 use 한다 

> use sua_database
switched to db sua_database
>
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
>
> db
sua_database

Q. 근데 왜 "show dbs" 명령에는 안보이는걸까

-> A. 최소 한개의 document 가 있어야 보인다 

 

 

 

 

 

collection 만들기

> show collections
>
> db.createcollection("t1");
uncaught exception: TypeError: db.createcollection is not a function :
@(shell):1:1
>
> db.createCollection("t1");
{ "ok" : 1 }
>
> show dbs
admin         0.000GB
config        0.000GB
local         0.000GB
sua_database  0.000GB
>
> show collections
t1

createCollection으로 collection(table)을 만든다

table 옵션(capped,autoIndex, size, max)을 줄 수 있다    

 

> show collections
t1

> db.xmlcollection.insert( {"test":"test"} );
WriteResult({ "nInserted" : 1 })

> show collections
t1
xmlcollection

collection 생성없이 insert 구문으로도 collection이 자동 생성될 수 있다

 

 

[root@server sua_database]# pwd
/mongod/db/sua_database

[root@sever sua_database]# ls
collection-10-4981004973008922723.wt  index-11-4981004973008922723.wt
collection-14-4981004973008922723.wt  index-15-4981004973008922723.wt

collection을 생성하면 storage 상에도 데이터가 저장되는데 , 디렉터리 위치는 '/etc/mongod.conf' 에서 확인하였다

 

 

data 넣기 

> db.t1.find()
>
> db.t1.insert({"name":"sua"});
WriteResult({ "nInserted" : 1 })
>
> db.t1.find()
{ "_id" : ObjectId("6641e4382704a1aa9525edd5"), "name" : "sua" }
>
> db.t1.find({name:"sua"})
{ "_id" : ObjectId("6641e4382704a1aa9525edd5"), "name" : "sua" }

db.[collection name].find() 명령으로  SELECT를 할 수 있다 

 

비정형 data insert

> db.t1.insert( [{userId:"01", name : {first:"sua" , last: "cho"} , age:"10" }, {userId:"02", name : "LeeJW"}])
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
>
> db.t1.find()
{ "_id" : ObjectId("6641e4382704a1aa9525edd5"), "name" : "sua" }
{ "_id" : ObjectId("6641e5ae2704a1aa9525edd6"), "userId" : "01", "name" : { "first" : "sua", "last" : "cho" }, "age" : "10" }
{ "_id" : ObjectId("6641e5ae2704a1aa9525edd7"), "userId" : "02", "name" : "LeeJW" }

## select 조건
- 안되는 상황
> db.t1.find({"first":"sua"})
>

- 되는 상황
> db.t1.find({"name.first":"sua"})
{ "_id" : ObjectId("6641e5ae2704a1aa9525edd6"), "userId" : "01", "name" : { "first" : "sua", "last" : "cho" }, "age" : "10" }