使用 java 驱动程序 3.2 更新 MongoDB 中的嵌套嵌入式数组文档
Updating nested embedded array document in MongoDB using java driver 3.2
我在 MongoDB 3.2 中有此文档。我无法更新我的嵌套数组文档
{
"_id" : ObjectId("5761c22edd93e211f49d5d51"),
"fullName" : "Mohammad Amir",
"enroll" : "abc123",
"email" : "amir.fragrance@gmail.com",
"mobile" : "8090370605",
"password" : "a$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
由于某种原因没有更新。
任何指点将不胜感激。
您需要以 profile.verification.0.provost 等方式访问数组元素,而不是 [0]。您在自己的示例中看到了效果,因为您在配置文件部分的末尾添加了一个子文档 "verification[0]"。
我在 MongoDB 3.2 中有此文档。我无法更新我的嵌套数组文档
{
"_id" : ObjectId("5761c22edd93e211f49d5d51"),
"fullName" : "Mohammad Amir",
"enroll" : "abc123",
"email" : "amir.fragrance@gmail.com",
"mobile" : "8090370605",
"password" : "a$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
由于某种原因没有更新。
任何指点将不胜感激。
您需要以 profile.verification.0.provost 等方式访问数组元素,而不是 [0]。您在自己的示例中看到了效果,因为您在配置文件部分的末尾添加了一个子文档 "verification[0]"。