当我在现有文档中编辑时,它会检索旧值而不是我的编辑值我该怎么做?

时间:2017-08-29 16:34:18

标签: java mongodb jsp

当我在现有文档中编辑时,它会检索旧值而不是我的编辑值我应该怎么做?

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

1 个答案:

答案 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);