본문 바로가기
[ Developer ]/MongoDB

[MongoDB] 몽고디비 스프링 Update

by 김현섭. 2016. 7. 3.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
Spring-Data-MongoDB Update 수정하기

@ Update Multi
업데이트를 하기 위해 먼저 메소드를 생성한다
파라미터는 key, value, 객체를 받아 바꿀 내용을 객체에 넣고 업데이트 시킨다

remove에 쓰였던 로직과 동일하게 Criteria를 이용해서 값을 찾고 Query에 넣는다


그러나 수정에서는 Update가 필요해서 객체를 만들고 변경할 값을 넣는다
이제는 메인에 가서 메소드를 이용한다 우선 removeAllDatas()를 이용해서 데이터를 먼저 지운다
그리고 나서 insertTestData() 즉 한 개의 데이터를 넣는 메소드를 5번 정도 실행 후 Update 시켜본다


객체를 생성할 때 에러가 발생한다면 밑에 선언한 MongoTestVO를 public static으로 접근지시자를 변경해준다


그러고 나서 실행을 하면 변경되어 있는 것을 볼 수 있다



*Update Multi - Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] args) {
        MongoTest mongoTest = new MongoTest();
        
        System.out.println(mongoTest.mongoTemplate);
        
        mongoTest.insertTestData();
        mongoTest.insertTestData();
        mongoTest.insertTestData();
        mongoTest.insertTestData();
        mongoTest.insertTestData();
        
        MongoTestVO mongoTestVO = new MongoTestVO();
        mongoTestVO.setName("도우너");
        mongoTestVO.setAddress("깐따삐야");
        
        mongoTest.updateDatas("name""둘리", mongoTestVO);
        //mongoTest.insertAllTestData();
        //mongoTest.removeAllDatas();
        //mongoTest.removeData("name", "둘리");
        
    }
cs

*Update Multi - Method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    private void updateDatas(String key, String value, MongoTestVO mongoTestVO) {
        
        Criteria criteria = new Criteria(key);
        criteria.is(value);
        
        Query query = new Query(criteria);
        
        Update update = new Update();
        update.set("name", mongoTestVO.getName());
        update.set("address", mongoTestVO.getAddress());
 
        mongoTemplate.updateMulti(query, update, "person");        
        
    }
cs


@ Update First
이제는 위와 같은 방식으로 1개를 수정하는 메소드를 작성해본다
기존의 메소드는 updateDatas로 바꾸고 updateData로 메소드를 생성한다


로직은 비슷하다고 볼 수 있다 
메인에서도 같다


그런 후 실행을 해본다


결과를 보면 첫 번째 데이터만 변경이 된 것을 볼 수 있다
Update First는 조건이 맞는 가장 첫 번째 데이터만을 변경하는 것이다 

*Update First - Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    public static void main(String[] args) {
        MongoTest mongoTest = new MongoTest();
        
        System.out.println(mongoTest.mongoTemplate);
        
        //mongoTest.insertTestData();
        
        MongoTestVO mongoTestVO = new MongoTestVO();
        mongoTestVO.setName("마이콜");
        mongoTestVO.setAddress("라면 좋아 라면");
        
        // Update First
        mongoTest.updateData("name""도우너", mongoTestVO);
        
        // Update Multi
        //mongoTest.updateDatas("name", "둘리", mongoTestVO);
        
        //mongoTest.insertAllTestData();
        //mongoTest.removeAllDatas();
        //mongoTest.removeData("name", "둘리");
        
    }
cs

*Update First - Method
1
2
3
4
5
6
7
8
9
10
11
12
    private void updateData(String key, String value, MongoTestVO mongoTestVO) {
        Criteria criteria = new Criteria(key);
        criteria.is(value);
        
        Query query = new Query(criteria);
        
        Update update = new Update();
        update.set("name", mongoTestVO.getName());
        update.set("address", mongoTestVO.getAddress());
        
        mongoTemplate.updateFirst(query, update, "person");
    }
cs