MongoDB 처음 설치 후 관리자를 등록하는 방법(보안 설정)


1. MongoDB 서버 실행

접근 제어 없이 실행

mongod.exe --port 27017 --dbpath "DB 경로"


2. MongoDB 클라이언트 실행

: 로그인 없이 MongoDB 서버 접속

mongo.exe --port 27017


3. 관리자 등록

use admin;


db.createUser({

    user: "관리자 ID",

    pwd: "관리자 암호",

    roles: [ { role: "root", db: "admin" } ]

});


4. MongoDB 서버 재실행

: 접근 제어 설정 (--auth 옵션)

mongod.exe --auth --port 27017 --dbpath "DB 경로"


5. MongoDB 클라이언트 실행

: 등록한 관리자 정보로 연결

mongo.exe --port 27017 -u "관리자ID" -p "관리자암호" "admin"


6. 작업용 사용자 추가



참고1> https://docs.mongodb.org/manual/tutorial/enable-authentication/#add-users-before-enabling-access-control


참고2> https://docs.mongodb.org/manual/tutorial/enable-authentication/#add-users-after-enabling-access-control



Posted by jungtae17
,

aggregation 처리 중 이전 stage 에서 나온 결과(배열A, 배열B)에서 배열A와 배열B를 합쳐서 새로운 배열을 만들어야 할 때 $concatArrays (aggregation) 오퍼레이터를 사용.


참고> https://docs.mongodb.org/manual/reference/operator/aggregation/concatArrays/


Posted by jungtae17
,

aggregation 처리 중 이전 stage 에서 나온 결과(배열)에서 새로운 배열을 만들어야 할 때 $map (aggregation) 오퍼레이터를 사용.



Posted by jungtae17
,

1. 설정 파일 만들기 (예: D:\DB\mongod.conf)

systemLog:

  destination: file

  path: "D:\\DB\\mongod.log"

  logAppend: true


storage:

  dbPath: "D:\\DB"

  directoryPerDB: true


setParameter:

  enableLocalhostAuthBypass: false


net:

  http:

    enabled: true

    RESTInterfaceEnabled: true


security:

  authorization: enabled


2. Windows 서비스 등록하기

mongod ^

  --install ^

  --serviceName "MongoDB" ^

  --serviceDisplayName "MongoDB(개발용)" ^

  --serviceDescription "개발용 MongoDB 입니다." ^

  --config "D:\DB\mongod.conf"


3. MongoDB 서비스 시작하기

NET START MongoDB



Posted by jungtae17
,

일정 시간이 지나면 데이터가 자동으로 삭제되는 인덱스. (만료 시간은 초(sec)단위 설정)


key 는 Date형 또는 Date형 데이터가 포함된 배열.


사용법:

db.collection.createIndex({"key1": 1[, "key2": 1, ...]}, {expireAfterSeconds: seconds});


참고> key 값이 갱신(update)되면 만료 시간도 연장 됨.



Posted by jungtae17
,

MongoDB 의 find() 쿼리 후 결과의 필드 이름을 바꾸고 싶을다면 Aggregation 을 이용하자.


aggregation 의 $project 스테이지에서 필드 이름을 지정할 수 있다.

단, 먼저 쿼리($match 스테이지) 실행한 결과에서 $project 스테이지를 실행한다.


db.game.aggregate([

  { $match: { _id: { $lte: 1000 } } },

  { $project: { _id: 0, "Rank": "$_id", Name: 1 } }

]);


위 코드는 '_id' 필드를 'Rank' 필드로 이름 바꿔서 결과를 반환한다.


참고로 _id 필드 이름을 실제로 바꾸는 것이 아니기 때문에 _id 필드를 포함하지 않도록 해야된다.


Posted by jungtae17
,

MongoDB 3.2 에서 추가된 $arrayElemAt (aggregation) 오퍼레이터를 사용해서 배열 요소를 꺼낼 수 있게 됨.


참고> https://docs.mongodb.org/manual/reference/operator/aggregation/arrayElemAt/


Posted by jungtae17
,

MongoDB 3.2 에서 추가된 $lookup (aggregation) 오퍼레이터를 사용해서 Join 처리를 할 수 있게 됨.


참고1> https://www.mongodb.com/blog/post/joins-and-other-aggregation-enhancements-coming-in-mongodb-3-2-part-1-of-3-introduction


참고2> https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/#pipe._S_lookup


Posted by jungtae17
,

{

  "event": "tennis",

  "teams": [

    {

      "name": "Red",

      "players": [

        { "name": "Kim", "age": 22 },

        { "name": "Lee", "age": 21 }

      ]

    },

    {

      "name": "Blue",

      "players": [

        { "name": "Park", "age": 23 },

        { "name": "Choi", "age": 19 }

      ]

    }

  ]

}


* 배열안에 들어있는 데이터 찾기 ('Blue' 팀 찾기)

db.game.find(

  { teams: { $elemMatch: { name: "Blue" } } },

  { _id: 0, "teams.$": 1 }

);

  결과>

{

  "teams": [

    {

      "name": "Blue",

      "players": [

        { "name": "Park", "age": 23 },

        { "name": "Choi", "age": 19 }

      ]

    }

  ]

}


* 배열안 배열에서 데이터 찾기 ('Park' 플레이어 찾기)

db.game.find(

  { teams: { $elemMatch: { players: { $elemMatch: { name: "Park" } } } } },

  { _id: 0, "teams.$": 1 }

);


Posted by jungtae17
,