大家好,我正在使用 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 中的数据