2014-03-25 3 views
2

"XX"라는 콜렉션이 있으며이 콜렉션에는 여러 문서가 있습니다. 필드 "userEmail"에 일치합니다. 내가 배열의 필드를 문서를 "USEREMAIL"= "[email protected]"찾아 업데이트해야 할 지금MongoDB의 배열 항목을 Java 드라이버를 사용하여 두 가지 "where"조건으로 업데이트하는 방법

{ "_id" : { "$oid" : "5331e4e313163623bb649249"} , 
    "userEmail" : "[email protected]"} 
    "reservations" : [ { 
     "idRest" : "23" , 
     "userPhone" : "88888888" , 
     "date" : "03-13-2015" , 
     "hour" : "14:30" 
    }, 
    { 
     "idRest" : "24" , 
     "userPhone" : "88888888" , 
     "date" : "03-13-2015" , 
     "hour" : "17:30" 
    }, 
    { 
     "idRest" : "22" , 
     "userPhone" : "88888888" , 
     "date" : "03-13-2015" , 
     "hour" : "16:30" 
    }] 

}

그리고 :

는 문서의 구조 여기서 "idRest"= 24입니다.이 경우 :

{ 
    "idRest" : "24" , 
    "userPhone" : "88888888" , 
    "date" : "03-13-2015" , 
    "hour" : "17:30" 
}, 

JAVA 드라이버를 사용하여 쿼리를 작성하는 데 도움을 줄 수 있습니까? :) 감사합니다.


편집 :이 내 코드, 대신 업데이트의 필드는 명령을 사용하여

`

쉘에서
DBCollection collReservationByUser = db.getCollection(usersReservationCollection); 

DBObject queryx = new BasicDBObject("userEmail", rest.getEmailUser()); 

BasicDBObject document1 = new BasicDBObject(); 

document1.put("idRest", rest.getEmailUser()); 
document1.put("userPhone", rest.getPhoneUser()); 
document1.put("date", rest.getFecha()); 
document1.put("hour", rest.getHora());; 

DBObject update1 = new BasicDBObject(); 
update1.put("$push", new BasicDBObject("reservations", document1)); 
collReservationByUser.update(queryx, update1, true, true);` 
+0

직접 작성하고 문제가 있습니까? 그렇다면 여기에 코드와 오류를 게시하십시오. –

+0

감사합니다. 코드를 추가합니다. 필드를 업데이트하는 대신 새 서브 문서를 추가하십시오 – HCarrasko

답변

2

, 당신이 할 수있는 새로운 하위 문서를 추가 like :

db.XX.update(
    // Find the document where [email protected] and find the element in the "reservations" array where idRest=24 
    { 
     "userEmail":"[email protected]", 
     "reservations.idRest":24 
    }, 
    // Update the element matched by the find criteria with new values 
    { 
     $set:{ 
      "reservations.$.idRest":99, 
      "reservations.$.userPhone":"888888", 
      "reservations.$.date":"03-13-2015", 
      "reservations.$.hour":"17:30" 
     } 
    } 
) 

그리고 자바와 동일하게 보일 것입니다 :

... 
    DBObject selectQuery = new BasicDBObject("userEmail", rest.getEmailUser()); 
    selectQuery.append("reservations.idRest", rest.getId()); 

    BasicDBObject updateFields = new BasicDBObject(); 
    updateFields.put("reservations.$.idRest", rest.getEmailUser()); 
    updateFields.put("reservations.$.userPhone", rest.getPhoneUser()); 
    updateFields.put("reservations.$.date", rest.getFecha()); 
    updateFields.put("reservations.$.hour", rest.getHora());; 

    DBObject updateQuery = new BasicDBObject(); 
    updateQuery.put("$set", updateFields); 
    collReservationByUser.update(selectQuery, updateQuery, true, true); 
    ... 

참고 : "예약"배열 "idRest"= 24 여러 요소가 경우, 상기 쿼리 에만 제 유사한 요소를 갱신한다. 위치 연산자를 사용하여 배열 내의 여러 요소를 업데이트 할 수 없습니다.

+0

도움을 주셔서 감사합니다 :) – HCarrasko

+0

그럼 비슷한 조건의 레코드를 검색하고 싶다면 Java 코드를 작성 하시겠습니까? –

0

업데이트도 크게 다르지 않습니다. 설명서의 .update() 메서드를 참조하십시오. 이 순서대로 positional $ 운영자에 의존

BasicDBObject document1 = new BasicDBObject(); 
document1.put("reservations.$.userPhone", rest.getPhoneUser()); 

DBObject update1 = new BasicDBObject(); 

update1.put("$set", document1); 
collReservationByUser.update(queryx, update1);` 

: 그냥이에 "userPhone"변화를 업데이트 할 수 있도록

DBObject queryx = new BasicDBObject("userEmail", "[email protected]"); 
queryx.put("idRest", 24); 


BasicDBObject document1 = new BasicDBObject(); 

document1.put("idRest", rest.getEmailUser()); 
document1.put("userPhone", rest.getPhoneUser()); 
document1.put("date", rest.getFecha()); 
document1.put("hour", rest.getHora()); 

DBObject update1 = new BasicDBObject(); 

update1.put("$set", new BasicDBObject("reservations.$", document1)); 
collReservationByUser.update(queryx, update1);` 

당신은 또한 모든 필드가 필요하지 않습니다하지만 당신은 대신 $set 연산자를 원할 것입니다 배열의 "매칭"색인 작업.

+0

코드가 꽤 잘되지만, 코드를 실행하면 아무런 변화가 없습니다 ... – HCarrasko

+0

@ Kramnik0 어떤 부분이 당신에게 문제입니까? 귀하의 문제는 * 당신이 업데이트하고 싶은 요소와 "일치"하는 것으로 보입니다. –

+0

예, 필드를 업데이트해야합니다. 도움을 주셔서 감사합니다.하지만 다른 대답은 내 문제에 대한 해결책이었습니다. – HCarrasko