2016-06-16 2 views
0

이 문서는 MongoDB 3.2에 있습니다. 나는Java 드라이버를 사용하여 MongoDB에서 중첩 된 임베디드 배열 문서 업데이트 3.2

{ 
    "_id" : ObjectId("5761c22edd93e211f49d5d51"), 
    "fullName" : "Mohammad Amir", 
    "enroll" : "abc123", 
    "email" : "[email protected]", 
    "mobile" : "8090370605", 
    "password" : "$2a$12$RTSx6T8SyY9d8d16HpgatuEUHwRBi60PZslCWvSGojNJSx21HKQuK", 
    "registeredOn" : ISODate("2016-06-15T21:01:34.736Z"), 
    "profile" : { 
     "isCompleted" : true, 
     "verification" : [ 
      { 
       "provost" : false, 
       "verifiedBy" : null, 
       "verifiedOn" : null 
      }, 
      { 
       "chairman" : false, 
       "verifiedBy" : null, 
       "verifiedOn" : null 
      } 
     ], 
     "isChecked" : false, 
     "fullName" : "Mohammad Amir", 
     "enroll" : "abc123", 
     "fatherName" : "jhgjgjh", 
     "motherName" : "kghjhgj", 
     "dob" : "2016-06-14T18:30:00.000Z", 
     "gender" : "Male", 
     "address" : "ytiutit", 
     "city" : "Abdul", 
     "state" : "Andaman and Nicobar Islands", 
     "country" : "Afghanistan", 
     "pincode" : "76575", 
     "courseType" : "UG", 
     "courseName" : "Adv Dip in Int.Decoration", 
     "semesterYear" : 1, 
     "facultyNumber" : "765gvjn", 
     "department" : "Agricultural Eco. Business Mngt.", 
     "hall" : "Abdullah Hall", 
     "verification[0]" : { 
      "provost" : true, 
      "verifiedBy" : "Director Computer Centre", 
      "verifiedOn" : ISODate("2016-06-16T19:37:33.161Z") 
     } 
    }, 
    "contact" : { 
     "emailID" : { 
      "isVerified" : false, 
      "isChecked" : false 
     }, 
     "mobileID" : { 
      "isVerified" : false, 
      "isChecked" : false 
     } 
    } 
} 

내가 업데이트되지 않는 몇 가지 이유로이

StringBuilder sb = new StringBuilder(); 
     BufferedReader br = request.getReader(); 
     String str = null; 
     while ((str = br.readLine()) != null) { 
      sb.append(str); 
     } 
     JSONObject jObj = new JSONObject(sb.toString()); 
     ObjectId _id = new ObjectId(jObj.getString("_id")); 
     boolean value = jObj.getBoolean("value"); 
     String mode = jObj.getString("mode"); 
     String verifiedBy = jObj.getString("verifiedBy"); 

     InputStream input = getServletContext().getResourceAsStream("/WEB-INF/config/config.properties"); 
     Properties properties = new Properties(); 
     properties.load(input); 
     String host = properties.getProperty("host"); 
     int port = Integer.parseInt(properties.getProperty("port")); 
     MongoClient mongoClient = new MongoClient(host, port); 
     MongoDatabase db = mongoClient.getDatabase("admin"); 

     MongoCollection<Document> coll = db.getCollection("students"); 
     Bson where = new Document().append("_id", _id); 
     Bson update = new Document() 
       .append("profile.verification[0].provost", value) 
       .append("profile.verification[0].verifiedBy", verifiedBy) 
       .append("profile.verification[0].verifiedOn", new Date()); 
     Bson set = new Document().append("$set", update); 
     try { 
      coll.updateOne(where, set, new UpdateOptions().upsert(true)); 
      List<Document> documents = (List<Document>) coll.find(where).into(new ArrayList<Document>()); 
      JSON json = new JSON(); 
      String serialize = json.serialize(documents); 
      response.getWriter().write(serialize); 
     } catch (MongoWriteException e) { 
      JSONObject json = new JSONObject(); 
      json.append("success", false); 
      json.append("class", "alert alert-success col-sm-8"); 
      json.append("message", "Something went wrong"); 
      String res = json.toString(); 
      response.getWriter().write(res); 
     } 
오류가 없습니다

하지만, profile.verification[0].provost = true처럼 업데이트 할 내 중첩 된 배열 문서를 업데이트 할 수 없습니다입니다.

모든 포인터가 감사하겠습니다.

답변

0

[0]이 아닌 profile.verification.0.provost 등으로 배열 요소에 액세스해야합니다. 프로필 부분의 끝에 "verification [0]"하위 문서를 추가하면 자신의 예제에서 효과를 볼 수 있습니다.

+0

뛰어난! 큰! 그것은 작동했습니다 ........ 당신은 내 시간을 저장합니다 감사합니다 mtj Logged –

관련 문제