2014-04-17 3 views
0

오브젝트 ID로 업데이트 문서를 보려고했지만 결과를 얻지 못했습니다. 여기 내 코드는 제발 도와주세요java에서 _id를 사용하여 mongodb에서 문서를 업데이트 할 수 있습니까?

DBCollection patients= db.getCollection("Patients"); 
    BasicDBObject doc = new BasicDBObject(); 
      doc.put("name","seshu"); 


DBObject update=`new` BasicDBObject().append("_id",ObjectId("534e1c8e40a8af540cd01ff4")); 

    patients`enter code here`.update(update, doc); 

답변

1

"결과가 나지 않습니다"라고 말하면 문서가 업데이트되지 않는다고 가정합니다.

컬렉션 이름, 데이터베이스 이름 및 ObjectId가 맞습니까? 그리고 그 ObjectId를 가진 컬렉션에 문서가 존재합니다. 프로그램이나 몽고 셸을 통해이 모든 것을 다시 확인해야합니다.

당신은 또한 몇 가지 추가 점검이 같은/코드의 디버깅, 뭔가 추가하지 않는 이유 :

DBCollection patients = db.getCollection("Patients"); 
DBObject update = new BasicDBObject().append("_id", new ObjectId("...")); 

long collectionCount = patients.count(); 
System.out.println(String.format("Collection count: %s", collectionCount)); 
long count = patients.count(update); 
System.out.println(String.format("Count for query: %s", count)); 

BasicDBObject doc = new BasicDBObject(); 
doc.put("name", "seshu"); 


WriteResult writeResult = patients.update(update, doc); 
System.out.println(String.format("Updated %s records", writeResult.getN())); 

DBObject updated = patients.findOne(update); 
System.out.println(updated); 
관련 문제