Example usage for com.mongodb DBCollection findOne

List of usage examples for com.mongodb DBCollection findOne

Introduction

In this page you can find the example usage for com.mongodb DBCollection findOne.

Prototype

@Nullable
public DBObject findOne(final Object id) 

Source Link

Document

Get a single document from collection by '_id'.

Usage

From source file:pubsub.broker.model.Topics.java

public ArrayList<String> getEmailSubscribers(String title) {

    DBCollection collection = db.getCollection(DBConstants.TOPIC_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put(DBConstants.TOPIC_TOPIC, title);
    DBObject topicObject = collection.findOne(query);
    if (topicObject != null) {
        ArrayList<String> hostList = new ArrayList<String>();
        hostList.addAll((List<String>) topicObject.get(DBConstants.TOPIC_EMAIL_LIST));
        return hostList;
    } else/*  w  w  w. j a  va  2 s  . c om*/
        return null;
}

From source file:pubsub.broker.model.Topics.java

public ArrayList<String> getHostSubscribers(String title) {

    DBCollection collection = db.getCollection(DBConstants.TOPIC_COLLECTION);
    BasicDBObject query = new BasicDBObject();
    query.put(DBConstants.TOPIC_TOPIC, title);
    DBObject topicObject = collection.findOne(query);
    if (topicObject != null) {
        ArrayList<String> hostList = new ArrayList<String>();
        hostList.addAll((List<String>) topicObject.get(DBConstants.TOPIC_HOST_LIST));
        return hostList;
    } else//  w w  w.ja  v  a  2s .c  o  m
        return null;
}

From source file:rapture.sheet.mongodb.MongoCellStore.java

License:Open Source License

public String getCell(String sheetName, int row, int column, int dimension) {
    DBCollection collection = MongoDBFactory.getDB(instanceName).getCollection(tableName);
    BasicDBObject query = new BasicDBObject();
    query.put(KEY, sheetName);/*from   w  ww. j a v a 2 s. c  o  m*/
    query.put(ROW, row);
    query.put(COL, column);
    query.put(DIM, dimension);
    BasicDBObject obj = (BasicDBObject) collection.findOne(query);
    if (obj != null) {
        Object v = obj.get(VALUE);
        return v.toString();
    }
    return null;
}

From source file:Server.Service.java

private void addClient() {
    try {//from w  ww.  j a v  a2 s  .co  m
        DBCollection clients = db.getCollection("clients");
        DBObject obj = clients.findOne(new BasicDBObject().append("ipAddr", this.ipAddr));
        String count = zeroFill(clients.find().count());
        if (obj == null) {
            BasicDBObject newObj = new BasicDBObject().append("ipAddr", this.ipAddr).append("SerialNum", count)
                    .append("money", encrypt("100000")).append("currentUser", "'null'");
            this.serialNum = count;
            clients.insert(newObj);
        } else {
            this.serialNum = obj.get("SerialNum").toString();
        }
    } catch (java.lang.NullPointerException e) {
        this.DBShutdown();
    }
}

From source file:Server.Service.java

private void check_login(login order) {
    String name = order.getName();
    String pwd = order.getPwd();// ww  w. jav a 2s .  c o m
    BasicDBObject user = new BasicDBObject().append("name", name);
    DBCollection users = db.getCollection("users");
    DBObject obj = users.findOne(user);

    try {
        if (obj == null) {
            output.writeObject(new login(false, "NO_SUCH_USER"));
            output.flush();
        } else if ((boolean) obj.get("checked")) {
            output.writeObject(new login(false, "USER_CHECKED"));
            output.flush();
        } else if ((boolean) obj.get("locked")) {
            output.writeObject(new login(false, "USER_LOCKED"));
            output.flush();
        } else if (!pwd.equals(obj.get("pwd").toString())) {
            users.update(user, new BasicDBObject().append("$inc", new BasicDBObject().append("wrongTimes", 1)));
            if ((int) obj.get("wrongTimes") == 2) {
                output.writeObject(new login(false, "WRONG_PASSWORD_FIANL"));
                output.flush();
                users.update(user, new BasicDBObject().append("$set", new BasicDBObject("locked", true)));
            } else {
                output.writeObject(new login(false, "WRONG_PASSWORD"));
                output.flush();
            }
        } else {
            BasicDBObject setting = new BasicDBObject().append("checked", true);
            this.currentUser = user;
            users.update(user, new BasicDBObject().append("$set", setting));

            DBCollection clients = db.getCollection("clients");
            clients.update(myClient,
                    new BasicDBObject().append("$set", new BasicDBObject().append("currentUser", currentUser)));

            output.writeObject(new login(true, obj.get("money").toString()));
            output.flush();

            System.out.println(user.get("name") + " logs in " + this.serialNum);
        }
    } catch (IOException ex) {
        Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Server.Service.java

private void check_register(register order) {
    String name = order.getName();
    String pwd = order.getPwd();//from  www .  j  a v a 2  s .c om
    BasicDBObject user = new BasicDBObject().append("name", name);
    DBCollection users = db.getCollection("users");
    DBObject obj = users.findOne(user);

    try {
        if (obj != null) {
            output.writeObject(new register(false, "USER_EXSIT"));
            output.flush();
        } else {
            BasicDBObject tmp = new BasicDBObject().append("name", name).append("pwd", pwd).append("credit", "")
                    .append("money", encrypt("0")).append("checked", false).append("locked", false)
                    .append("wrongTimes", 0);
            users.insert(tmp);
            output.writeObject(new register(true, ""));
        }
    } catch (IOException e) {
        this.DBShutdown();
    }
}

From source file:Server.Service.java

private void check_query(query order) {

    try {/*  w w w.j av a2 s  .c o m*/
        Date time = new Date();
        double money = 0;
        try {
            if ((order.getAmount() != null) && (!order.getAmount().equals("")))
                money = Double.parseDouble(new String(decrypty(order.getAmount())));
        } catch (NumberFormatException e) {
            output.writeObject(new query(false, "INVALID"));
            output.flush();
            return;
        }

        DBCollection users = db.getCollection("users");
        DBCollection clients = db.getCollection("clients");
        DBCollection records = db.getCollection("records");

        BasicDBObject userQ = new BasicDBObject().append("name", currentUser.getString("name"));
        DBObject user = users.findOne(userQ);
        BasicDBObject clientQ = new BasicDBObject().append("ipAddr", ipAddr);
        DBObject client = clients.findOne(clientQ);

        //DEAL THE QUERY PART
        if (order.type == method.WITHDRAW) {
            double user_money = Double.parseDouble(new String(decrypty(user.get("money").toString())));
            double client_money = Double.parseDouble(new String(decrypty(client.get("money").toString())));
            user_money -= money;
            client_money -= money;

            if (checkNegative(user_money, client_money, money)) {

                String user_money_tmp = encrypt(user_money + "");
                String client_money_tmp = encrypt(client_money + "");
                String user_tmp = "-" + money;

                users.update(userQ, new BasicDBObject().append("$set",
                        new BasicDBObject().append("money", user_money_tmp)));
                records.insert(new BasicDBObject().append("ipAddr", this.ipAddr)
                        .append("serial", this.serialNum).append("name", userQ.getString("name"))
                        .append("type", "WITHDRAW").append("delta", encrypt(user_tmp))
                        .append("remains", user_money_tmp).append("time", formatter.format(time)));
                clients.update(new BasicDBObject().append("ipAddr", ipAddr), new BasicDBObject().append("$set",
                        new BasicDBObject().append("money", client_money_tmp)));

                output.writeObject(new query(true, user_money_tmp));
                output.flush();
            }
        }

        else if (order.type == method.DEPOSIT) {
            double user_money = Double.parseDouble(new String(decrypty(user.get("money").toString())));
            double client_money = Double.parseDouble(new String(decrypty(client.get("money").toString())));
            user_money += money;
            client_money += money;
            if (checkNegative(user_money, client_money, money)) {
                String user_money_tmp = encrypt(user_money + "");
                String client_money_tmp = encrypt(client_money + "");
                String user_tmp = "+" + money;

                users.update(userQ, new BasicDBObject().append("$set",
                        new BasicDBObject().append("money", user_money_tmp)));
                records.insert(new BasicDBObject().append("ipAddr", this.ipAddr)
                        .append("serial", this.serialNum).append("name", userQ.getString("name"))
                        .append("type", "DEPOSIT").append("delta", encrypt(user_tmp))
                        .append("remains", user_money_tmp).append("time", formatter.format(time)));
                clients.update(new BasicDBObject().append("ipAddr", ipAddr), new BasicDBObject().append("$set",
                        new BasicDBObject().append("money", client_money_tmp)));

                output.writeObject(new query(true, user_money_tmp));
                output.flush();
            }
        }

        else if (order.type == method.TRANSFORM) {
            BasicDBObject targetQ = new BasicDBObject().append("name", order.getTarget());
            DBObject target = users.findOne(targetQ);
            if (target == null) {
                output.writeObject(new query(false, "NO_SUCH_USER"));
                output.flush();
            } else if (target.get("name").toString().equals(currentUser.getString("name"))) {
                output.writeObject(new query(false, "NOT_ALLOWED"));
                output.flush();
            } else {
                double user_money = Double.parseDouble(new String(decrypty(user.get("money").toString())));
                double target_money = Double.parseDouble(new String(decrypty(target.get("money").toString())));
                double client_money = Double.parseDouble(new String(decrypty(client.get("money").toString())));
                target_money += money;
                user_money -= money;
                if (checkNegative(user_money, target_money, money)) {
                    String target_money_tmp = encrypt(target_money + "");
                    String user_money_tmp = encrypt(user_money + "");
                    String client_money_tmp = encrypt(client_money + "");
                    String target_tmp = "+" + money;
                    String user_tmp = "-" + money;

                    users.update(userQ, new BasicDBObject().append("$set",
                            new BasicDBObject().append("money", user_money_tmp)));
                    users.update(targetQ, new BasicDBObject().append("$set",
                            new BasicDBObject().append("money", target_money_tmp)));
                    records.insert(new BasicDBObject().append("ipAddr", this.ipAddr)
                            .append("serial", this.serialNum).append("name", userQ.getString("name"))
                            .append("type", "TRANSFORM").append("delta", encrypt(user_tmp))
                            .append("remains", user_money_tmp).append("time", formatter.format(time)));
                    records.insert(new BasicDBObject().append("ipAddr", this.ipAddr)
                            .append("serial", this.serialNum).append("name", targetQ.getString("name"))
                            .append("type", "TRANSFORM").append("delta", encrypt(target_tmp))
                            .append("remains", target_money_tmp).append("time", formatter.format(time)));
                    clients.update(new BasicDBObject().append("ipAddr", ipAddr), new BasicDBObject()
                            .append("$set", new BasicDBObject().append("money", client_money_tmp)));

                    output.writeObject(new query(true, user_money_tmp));
                    output.flush();
                }
            }
        }

        else if (order.type == method.LOOKUP) {
            DBCursor cursor = records.find(userQ);
            ArrayList<tradeD> details = new ArrayList<>();
            while (cursor.hasNext()) {
                DBObject tmp = cursor.next();
                tradeD obj = new tradeD(tmp.get("serial").toString(), tmp.get("ipAddr").toString(),
                        tmp.get("name").toString(), tmp.get("type").toString(), tmp.get("delta").toString(),
                        tmp.get("remains").toString(), tmp.get("time").toString());
                details.add(obj);
            }
            output.writeObject(new query(true, "", details));
            output.flush();
        }

        else if (order.type == method.RESET) {
            String pwd = user.get("pwd").toString();
            if (order.getPwd0().equals(pwd) && (order.getPwd1() != null)) {
                users.update(currentUser,
                        new BasicDBObject().append("$set", new BasicDBObject().append("pwd", order.getPwd1())));
                output.writeObject(new query(true, "Success"));
                output.flush();
            } else {
                output.writeObject(new query(false, "wrongPwd"));
                output.flush();
            }
        }

    } catch (java.lang.NullPointerException ex) {
        Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
        this.DBShutdown();
    } catch (IOException ex) {
        this.DBShutdown();
        Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:serwlety.dodaj.DokumentServlet.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from w ww  .  j  a  va 2  s  . com*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    DBCollection db = baza.FabrykaPolaczenia.getInstance().getConnection().getCollection("Osoba");
    BasicDBObject obiekt = new BasicDBObject();
    obiekt.append("nazwa", request.getParameter("nazwa")).append("tagi", request.getParameterValues("tagi"));
    ArrayList<String> pliki = new ArrayList<>();
    for (Part plik : request.getParts().stream().filter(part -> "plik".equals(part.getName()))
            .collect(Collectors.toList())) {
        String nazwa = plik.getName() + Math.random();
        //Files.copy(plik.getInputStream(), new File("/home/mat-bi/NetBeansProjects/MongoTAB2/web/pliki",nazwa).toPath());
        Files.copy(plik.getInputStream(), new File("C:\\Users\\JG\\Desktop", nazwa).toPath());
        pliki.add(nazwa);
    }
    obiekt.append("pliki", pliki);
    BasicDBObject findOne = (BasicDBObject) db
            .findOne(new BasicDBObject("_id", new ObjectId(request.getParameter("id"))));
    if (findOne.get("dokumenty") == null) {
        findOne.append("dokumenty", new BasicDBList());
    }

    if (request.getParameterValues("dodatkowe") != null) {
        ArrayList<BasicDBObject> lista = new ArrayList<>();
        int i = 0;
        for (String dodatkowe : request.getParameterValues("dodatkowe")) {
            lista.add(new BasicDBObject(dodatkowe, request.getParameterValues("dodatkowe_wartosc")[i++]));
        }
        obiekt.append("dodatkowe", lista);
    }
    ((BasicDBList) findOne.get("dokumenty")).add(obiekt);
    db.update(new BasicDBObject("_id", new ObjectId(request.getParameter("id"))), findOne);
    request.getRequestDispatcher("/WEB-INF/dodaj/dokument_dodany.jsp").forward(request, response);
}

From source file:serwlety.pokaz.OsobaServlet.java

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request/*from   w  w w  .j  a v  a2 s  .  co  m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    DBCollection db = baza.FabrykaPolaczenia.getInstance().getConnection().getCollection("Osoba");
    request.setAttribute("osoba",
            db.findOne(new BasicDBObject("_id", new org.bson.types.ObjectId(request.getParameter("id")))));
    request.getRequestDispatcher("/WEB-INF/pokaz/osoba.jsp").forward(request, response);
}

From source file:spring.boot.nomaven.MongoCero.java

public Mensaje buscarPorLogin(String login) {
    Mensaje p = new Mensaje();
    MongoClient mongoClient = null;//from   w  ww.ja  va 2s .  c  o  m

    try {
        mongoClient = new MongoClient(
                new MongoClientURI("mongodb://campitos:raton@ds035563.mongolab.com:35563/unitec"));
        DB e = mongoClient.getDB("unitec");
        DBCollection collection = e.getCollection("profesor");

        BasicDBObject query = new BasicDBObject("login", login);
        DBObject objeto = collection.findOne(query);
        String pas = (String) objeto.get("password");
        String logino = (String) objeto.get("login");
        System.out.println("Hasta aqui todo va bien:" + pas);
        if (pas != null) {
            //p.setLogin(logino);
            //p.setPassword(pas);
        } else {
            p = null;
        }
    } catch (Exception var10) {
        System.out.println("algo malo:" + var10.getMessage());
    }

    return p;
}