当我在现有文档中编辑时,它会检索旧值而不是我的编辑值我应该怎么做?
BasicDBObject newDocument=new BasicDBObject();
BasicDBObject OldDocument=new BasicDBObject();
BasicDBObject upd=new BasicDBObject();
String value = request.getParameter("_id");
String name = session.getAttribute("un").toString();
System.out.println(name);
newDocument.put("un",user.getName());
OldDocument.put("name",name);
upd.put("$set",newDocument);
coll.update(OldDocument,upd);
答案 0 :(得分:1)
您需要使用{new:true}选项来检索最新更新的文档
BasicDBObject newDocument=new BasicDBObject();
BasicDBObject OldDocument=new BasicDBObject();
BasicDBObject upd=new BasicDBObject();
BasicDBObject opt=new BasicDBObject();
String value = request.getParameter("_id");
String name = session.getAttribute("un").toString();
System.out.println(name);
newDocument.put("un",user.getName());
OldDocument.put("name",name);
upd.put("$set",newDocument);
opt.put("new",true);
coll.update(OldDocument,upd,opt);