如何在Java中从mongodb获取单个字段数据

时间:2019-03-19 10:30:01

标签: mongodb session cursor

我正在尝试使用MongoDB作为后端数据库来实现JSP Web应用程序,并在检索到的数据的“用户名”字段上设置会话。This is my data in the mongo db

以下是我的servlet代码,

公共BasicDBObject authenticateUser(LoginBean loginBean){

    String userName = loginBean.getUserName();
    String password = loginBean.getPassword();


    Mongo mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("MovieRecommender");

    DBCollection table = db.getCollection("Users");

    BasicDBObject andQuery = new BasicDBObject();
    List<BasicDBObject> obj = new ArrayList<>();
    obj.add(new BasicDBObject("Username", userName));
    obj.add(new BasicDBObject("Password", password));
    andQuery.put("$and", obj);


    DBCursor cursor = table.find(andQuery);



    while (cursor.hasNext()) {
         /**
          * This is where my session has to be implemented for the Username
          */


    }        
}

0 个答案:

没有答案