您好,我正在使用Java在mongoDB上工作。我成功完成了连接插入并从mongoDB检索值,但是当我尝试更新数据时,我的代码无法正常工作。
我的代码是:
public static Result updateprofile() throws UnknownHostException
{
final DynamicForm profileform=form().bindFromRequest();
final String username1 = profileform.get("username");
final String password1 = profileform.get("password");
final String email1 = profileform.get("email");
final String userid = session("userid");
MongoClient mongo=new MongoClient("localhost",27017);
DB db = mongo.getDB("webportal");
DBCollection coll=db.getCollection("userdb");
BasicDBObject doc2=new BasicDBObject("_id", userid);
BasicDBObject doc1=new BasicDBObject();
doc1.append("username", username1);
doc1.append("username", username1);
doc1.append("password", password1);
doc1.append("email",email1 );
BasicDBObject doc3=new BasicDBObject("$set",doc1);
coll.update(doc2, doc3);
return ok(userid+username1);
}
我阅读了此块以更新mongo中的数据
请尝试一下。
ObjectId id= new ObjectId("4f693d40e4b04cde19f17205");
BasicDBObject searchQuery = new BasicDBObject();
searchQuery .put("_id", id);
BasicDBObject updateDocument = new BasicDBObject();
updateDocument .append("$set", new BasicDBObject("username", username1));
coll.update(searchQuery , updateDocument);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句