본문 바로가기

TIL

TIL) EPSILON, 몽고 업데이트시 다른 컬럼 값 참조, 몽고 덤프

1. Epsilon

테스트 코드에 엡실론이라는 단어가 나와서 살짝 당황했다.

엡실론 : 테스트 코드에서, 소숫점 결과를 검증할 때 차이가 몇 이하로 계산되는가 ? 를 검증한다고 치면, 몇 이하 의 몇 을 앱실론이라 하는 듯 하다.

 

[AssertJ]
val EPSILON = 1e-8

assertThat(Actural)
    .isEqualTo(Expected, within(EPSILON))

assertThat(Actural)
    .isCloseTo(Expected, within(EPSILON))
// Actural 과 Expected 의 값이 EPSILON 이하인가.
// isEqualTo 와 isCloseTo 의 결과는 같다.

assertThat(8.1).isEqualTo(8.0, within(0.2)) // pass
assertThat(8.4).isEqualTo(8.0, within(0.2)) // fail

 

assertJ 에서 assertThat() 메서드는 Assertions 클래스에 있다.

 

반면, isEqualTo() 는 비교하는 대상에 따라 클래스가 나눠져 있는데,

 

엡실론을 통한 소숫점 비교 케이스에서는

  • Double : FloatingPointNumberAssert 인터페이스를 구현한 AbstractDoubleAssert
  • BigDecimal : AbstractBigDecimalAssert

의 isEqualTo(), isCloseTo() 를 사용한다.

여기 메서드들의 주석을 보면 isEqualTo() = isCloseTo() 결과가 같다고 한다.

그래도 문맥적 의미가 더 정확한 isCloseTo() 를 쓰자.

 

FloatingPointNumberAssert API

http://joel-costigliola.github.io/assertj/core/api/org/assertj/core/api/FloatingPointNumberAssert.html

 

FloatingPointNumberAssert (AssertJ fluent assertions 2.9.1 API)

Verifies that the actual number is close to the given one within the given offset value. This assertion is the same as isCloseTo(Number, Offset). When abs(actual - expected) == offset value, the assertion: Examples: // assertions will pass assertThat(8.1).

joel-costigliola.github.io

 

 

 


 

 

 

2. MongoDb 업데이트시 다른 컬럼 값 참조

mongoDB 에서 업데이트를 하는 방법은 아래와 같다.

db.employee.updateMany(
  {
    alive : true, job : 'programmer'
  },
  {
    $set : { salary : { $multiply : ["$salary", "$age"] } }
  }
)

연봉을 [현재 연봉 * 나이] 로 올려버렸다.

 

하지만 이 쿼리는 동작하지 않는다.

에러난다.

 

db.employee.updateMany(
  {
    alive : true, job : 'programmer'
  },
  {
    $set : { salary : "$age" }
  }
)

이렇게 하면, salary 값은 모두 $age 문자열 자체가 들어간다.

 

 

[해결]

mongoDb 4.2 버전 이상을 쓴다면 set 중괄호를 대괄호로 묶으면 된다.

db.employee.updateMany(
  {
    alive : true, job : 'programmer'
  },
  [{
    $set : { salary : { $multiply : ["$salary", "$age"] } }
  }]
)

 

 

이하 버전은 아래글을 참조하자.

https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field

 

Update MongoDB field using value of another field

In MongoDB, is it possible to update the value of a field using the value from another field? The equivalent SQL would be something like: UPDATE Person SET Name = FirstName + ' ' + LastName And ...

stackoverflow.com

 

 

 


 

 

 

3. 몽고 덤프

몽고DB 의 덤프를 만들어서 복사하거나 해야할 일이 종종 있다.

 

mongodump --host [mongo 주소] -u [접속 아이디] -p [비밀번호] 
	-d [디비 이름] --out [덤프를 저장할 위치] 
	--collection [컬렉션 이름(없을시 전체)] --query "[쿼리]"

이렇게 하면 몽고 덤프 파일이 생성된다.

 

https://docs.mongodb.com/database-tools/mongodump/

 

mongodump — MongoDB Database Tools

Docs Home → MongoDB Database Toolsmongodump is a utility for creating a binary export of the contents of a database. mongodump can export data from either mongod or mongos instances; i.e. can export data from standalone, replica set, and sharded cluster

docs.mongodb.com

 

 

 

 

덤프 파일을 다시 붙여넣는 방법은 아래가 예시다.

mongorestore -h [덤프 파일을 복사할 호스트(127.0.0.1)] -u [아이디] -p [비밀번호] 
	-d [디비 이름] -c [저장할 컬렉션 이름(디비에 없는 컬렉션인 경우 새로 만든다)] 
    --noIndexRestore [여러가지 옵션이 있다. 이건 인덱스를 복사하지 않는것.]
    mongobackup/backup.bson
    
// 마지막에 덤프파일의 경로를 적는다.

 

 

아래 공식 홈페이지에서 리스토어시 여러 옵션을 볼 수 있다.

https://docs.mongodb.com/database-tools/mongorestore/

 

mongorestore — MongoDB Database Tools

Docs Home → MongoDB Database ToolsThe mongorestore program loads data from either a binary database dump created by mongodump or the standard input into a mongod or mongos instance.Run mongorestore from the system command line, not the mongo shell.See al

docs.mongodb.com

 

 

 

 

'TIL' 카테고리의 다른 글

TIL) Kotlin Test  (0) 2022.02.26
TIL) Airflow Xcom  (0) 2022.02.25
TIL) 인덱스, DFA  (0) 2022.02.23
TIL) Webserver vs WAS, NGNIX vs Apache  (0) 2022.02.22
TIL) associate 시리즈  (0) 2022.02.19