2016-06-16 12 views
0

このドキュメントはMongoDB 3.2にあります。私はそこにはエラーはありませんが、何らかの理由でprofile.verification[0].provost = trueが更新されていない Javaドライバを使用してMongoDBのネストされた埋め込み配列ドキュメントを更新する3.2

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); 
     } 

このようにそれを更新したい私のネストされた配列のドキュメント

{ 
    "_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 
     } 
    } 
} 

を更新することができません。

何か指摘していただければ幸いです。

答えて

0

[0]ではなく、profile.verification.0.provostなどとして配列要素にアクセスする必要があります。プロファイルパートの最後にサブ文書 "verification [0]"を追加したので、独自の例でその効果を確認できます。

+0

優秀!すばらしいです!それは働いた........あなたは私の時間を保存するありがとうmtj –

関連する問題