我在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);
}
我想像这样更新
profile.verification[0].provost = true
没有错误,但def multiply_number_in_context(match):
return "{0}{1}{2}".format(match.group(1), str(float(match.group(2))*3), '"')
editor.rereplace(r'("StackSize" Value=")(\d+)"', multiply_number_in_context)
由于某种原因没有更新。
任何指针都会受到赞赏。
答案 0 :(得分:0)
您需要将数组元素作为profile.verification.0.provost等访问,而不是[0]。您在自己的示例中看到效果,因为您添加了子文档&#34;验证[0]&#34;在个人资料部分的末尾。