[MongoDB] MongoDB

Younghwan Cha·2023년 3월 9일
0

Database

목록 보기
6/16
post-thumbnail

[install MongoDB] https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu/

# start mongoDB server 
mongod 

# start mongoDB shell
mongosh

commands

use <DB Name>

# 현재 생성되어 있는 DB 반환
show dbs

# 현재 사용하고 있는 DB 반환
db

# 현재 생성되어 있는 collection 반환
show collections

# collection 에 들어가있는 값 반환
db.<Collection Name>.find()

# collection 제거
db.<Collection Name>.drop()

# DB 제거
<DB Name>.dropDatabase()

$lookup

$lookup: {
  from: '가져오려는 table',
  localField: 'reference key',
  foreignField: 'foreign key',
  as: '지정할 이름',
},

===================
// 아래 처럼 lookup 한 결과에 대한 lookup 을 진행 할 수도 있다.
  
{
  $lookup: {
    from: 'testtagmodels',
    localField: '_id',
    foreignField: 'testId',
    as: 'testTags',
  },
},
{
  $lookup: {
    from: 'tagmodels',
    localField: 'testTags.tagId',
    foreignField: '_id',
    as: 'testTags.tagDetails',
  },
},

$geoNear

$geoNear 작업을 수행하려면 MongoDB 컬렉션에 2D 또는 2Dsphere index 가 필요하다.

# schema.prisma
  model test {
  	id       string  @db.ObjectId()
    location Point                    // Point 형식의 필드로 지정
    
  	@@index([location], name: "idx_location", map: "2dsphere")
  }

[docs] https://www.mongodb.com/docs/manual/reference/operator/aggregation/geoNear/

$unwind

$unwind 는 배열을 풀어서 각 요소를 별도의 문서로 만든다. 예시를 보면 이해가 쉽다.

$unwind: $history 실행 전

[
  {
    "_id": 1,
    "academy": "A",
    "history": [
      {
        "_id": "abc",
        "user": "user1",
        "court": "court1"
      },
      {
        "_id": "def",
        "user": "user2",
        "court": "court2"
      }
    ]
  },
]

$unwind: $history 실행 후

[
  {
    "_id": 1,
    "academy": "A",
    "history": {
      "_id": "abc",
      "user": "user1",
      "court": "court1"
    }
  },
  {
    "_id": 1,
    "academy": "A",
    "history": {
      "_id": "def",
      "user": "user2",
      "court": "court2"
    }
  },

]

DB Dump


mongoDB 의 database dump 는 어떻게 생성할까?

mongodump

mongodump 명령어는 MongoDB 데이터베이스의 데이터를 백업하기 위해 사용되며, mongodump 을 실행하면 MongoDB 서버에서 데이터를 백업하여 BSON 형식의 데이터 파일로 저장한다.

$ mongodump
	--host <hostname>
    --port <port> 
    --username <username> 
    --password <password> 
    --db <databaseName> 
    --out <backupDirectory>
    
$ mongodump 
    --uri=<mongoDB uri>
    --db=<DB to use>
    --archive=<dump as an archive to the specified path.
              If flag is specified without a value, archive is written to stdout>

mongorestore

mongorestore 명령어는 mongodump 으로 백업한 데이터를 MongoDB에 다시 복원하는 데 사용된다.

$ mongorestore
	--host <hostname>
    --port <port>
    --username <username> 
    --password <password> 
    --db <databaseName> <backupDirectory>
    
$ mongorestore
	--archive=<restore dump from the specified archive file.
               If flag is specified without a value, archive is read from stdin>
    --nsFrom=<--archive 에서 가져와서 복원할 데이터를 database.collection 형식으로 기입>
    --nsTo=<복원할 데이터가 저장될 경로를 database.collection 형식으로 기입> 
    --uri=<mongoDB uri>

중복되는 컬럼 로우 확인

db.settlementmodels.aggregate([
    {
        $group: {
            _id: { transactionKey: "$transactionKey" },
            count: { $sum: 1 },
        }
    },
    {
        $match: {
            count: { $gt: 1 }
        }
    }
])

Pricing

mongoDB pricing

profile
개발 기록

0개의 댓글