일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 백준 10951번 c
- 백준 10950번
- 2020 펭수 달력
- 이클립스 알고리즘 환경
- Eclipse Althrithm
- 자바스크립트
- 포항 지진
- 펭수 달력
- 티스토리 초대장 이벤트
- 백준 10951번 java
- 백준 10950번 java
- RETURN ROW IF NO DATA FOUND
- 백준 1000번
- 이클립스 알고리즘 세팅
- 배열 복사
- 펭수 2020 달력
- 백준 알고리즘
- 백준 1000번 c++
- 오라클
- JavaScript
- 백준 10951번 c++
- 백준10950번 c++
- nodejs
- 백준 10951번
- 백준 1000번 java
- 백준 10950번 c
- 백준 1000번 c
- oracle
- 티스토리 초대장
- 지진
- Today
- Total
스노우보드 참 좋아하는데 맨날 키보드 앞에만 있네
[MongoDB] MongoDB 실행, 데이터베이스 생성/제거, Collection 생성/제거 본문
MongoDB 실행, 데이터베이스 생성/제거,
Collection 생성/제거
오늘은 MongoDB를 실행하고, 데이터베이스와 Collection을 생성한 후 제거해보았다.
noSQL이기 때문에 기존 SQL을 사용하여 생성하던 방식과 차이가 있다.
서버 실행 및 접속
먼저 MongoDB 서버를 실행한다.
터미널을 켜서 'mongod' 명령어를 입력하여 서버를 실행한다.
서버가 실행되면서 pid, port번호, dbpath가 출력된다.
|
$ mongod
MongoDB starting : pid=1234 port=12345 dbpath=/data/db 64-bit host=username.local |
|
'mongo' 명령어를 입력하여 서버에 접속한다.
url서버 url과 서버 버전이 표기된다.
|
$ mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10 |
|
데이터베이스 생성 : use
데이터베이스 생성은 'use DATABASE_NAME' 명령어로 할 수 있다.
우선 현재 사용중인 데이터베이스를 보기위해 'db'명령어를 입력한다.
|
> db
test
> use mongodb_tutorial
switched to db mongodb_tutorial
> db
mongodb_tutorial |
|
기본적으로는 test라는 이름의 데이터베이스가 실행된다.
'use mongodb_tutorial' 명령어를 통해 mongodb_tutorial 이라는 데이터베이스를 생성했다.
이후 다시 db 명령어를 통해 생성한 데이터베이스가 실행된 것을 볼 수 있다.
|
> show dbs
admin 0.000GB
local 0.000GB |
|
'show dbs' : 내가 만든 데이터베이스 리스트를 확인한다.
리스트에서 데이터베이스를 보려면 최소 한개 이상의 Document를 추가해야 한다.
아래와 같이 Document를 추가하면, 추가된 mongodb_tutorial 데이터베이스를 볼 수 있다.
|
> db.book.insert({"name": "MongoDB Tutorial", "autohr": "velopert"});
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
local 0.000GB
mongodb_tutorial 0.000GB |
|
데이터베이스 제거 : delete
데이터베이스 제거에 앞서 'use' 명령어로 제거할 데이터베이스를 선택한다.
이후 db.dropDatabase(); 명령어로 데이터베이스를 제거한다.
|
> use mongodb_tutorial
switched to db mongodb_tutorial
> db.dropDatabase();
{ "dropped" : "mongodb_tutorial", "ok" : 1 } |
|
Collection 생성 : db.createCollection();
Colletion을 생성하기 위해 db.createCollection() 명령어를 사용한다.
db.createCollection() 의 메소드는 name, options가 있다.
name은 생성하려는 컬렉션의 이름이며, options 변수는 필요에 따라 사용하고, 생략 가능하다.
Parameter |
Type |
Description |
name |
string |
The name of the collection to create. |
options |
document |
Optional. Configuration options for creating a capped collection, for preallocating space in a new collection, of tor creating a view/. |
options 에 들어갈 수 있는 필드는 다음과 같다.
작성하려다 필드 수가 너무 많아 mongodb DOCS 에서 복붙하였다.
Field | Type | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
capped |
boolean | Optional. To create a capped collection, specify true . If you specify true , you must also set a maximum size in the size field. | ||||||||
autoIndexId |
boolean |
Optional. Specify IMPORTANT For replica sets, do not set Deprecated since version 3.2. | ||||||||
size |
number | Optional. Specify a maximum size in bytes for a capped collection. Once a capped collection reaches its maximum size, MongoDB removes the older documents to make space for the new documents. The size field is required for capped collections and ignored for other collections. | ||||||||
max |
number | Optional. The maximum number of documents allowed in the capped collection. The size limit takes precedence over this limit. If a capped collection reaches the size limit before it reaches the maximum number of documents, MongoDB removes old documents. If you prefer to use the max limit, ensure that the size limit, which is required for a capped collection, is sufficient to contain the maximum number of documents. | ||||||||
usePowerOf2Sizes |
boolean |
Optional. Available for the MMAPv1 storage engine only. Deprecated since version 3.0: For the MMAPv1 storage engine, all collections use the power of 2 sizes allocation unless the | ||||||||
noPadding |
boolean |
Optional. Available for the MMAPv1 storage engine only. New in version 3.0: Defaults to WARNING Do not set | ||||||||
storageEngine |
document |
Optional. Available for the WiredTiger storage engine only. New in version 3.0. Allows users to specify configuration to the storage engine on a per-collection basis when creating a collection. The value of the { <storage-engine-name>: <options> }
Storage engine configuration specified when creating collections are validated and logged to the oplogduring replication to support replica sets with members that use different storage engines. | ||||||||
validator |
document |
Optional. Allows users to specify validation rules or expressions for the collection. For more information, see Document Validation. New in version 3.2. The NOTE
| ||||||||
validationLevel |
string |
Optional. Determines how strictly MongoDB applies the validation rules to existing documents during an update. New in version 3.2.
| ||||||||
validationAction |
string |
Optional. Determines whether to New in version 3.2. IMPORTANT Validation of documents only applies to those documents as determined by the
| ||||||||
indexOptionDefaults |
document |
Optional. Allows users to specify a default configuration for indexes when creating a collection. The { <storage-engine-name>: <options> }
Storage engine configuration specified when creating indexes are validated and logged to the oplog during replication to support replica sets with members that use different storage engines. New in version 3.2. | ||||||||
viewOn |
string |
The name of the source collection or view from which to create the view. The name is not the full namespace of the collection or view; i.e. does not include the database name and implies the same database as the view to create. See also New in version 3.4. | ||||||||
pipeline |
array |
An array that consists of the aggregation pipeline stage. The view definition is public; i.e. See also New in version 3.4. | ||||||||
collation |
document |
Specifies the default collation for the collection. Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. The collation option has the following syntax: collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
When specifying collation, the If you specify a collation at the collection level:
If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons. For a collection, you can only specify the collation during the collection creation. Once set, you cannot modify the collection’s default collation. For an example, see Specify Collation. New in version 3.4. |
'개발 > NodeJS' 카테고리의 다른 글
[NodeJS] Winston 모듈로 만드는 Logger (0) | 2017.11.15 |
---|---|
[NodeJS] 맥북에 MongoDB 설치하기 (0) | 2017.11.09 |
[NodeJS] 비동기 프로그래밍 (0) | 2017.11.08 |