Example usage for com.mongodb DBCursor count

List of usage examples for com.mongodb DBCursor count

Introduction

In this page you can find the example usage for com.mongodb DBCursor count.

Prototype

public int count() 

Source Link

Document

Counts the number of objects matching the query.

Usage

From source file:TeacherOne.java

private void GetdataBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GetdataBtnActionPerformed
    // TODO add your handling code here:
    stuFirst = FirstnameField.getText();
    stuMid = MidnameField.getText();//from   w  w  w.j  a va2s  . com
    stuLast = LastnameField.getText();
    grade = Integer.parseInt(GradeField.getText());
    section = Integer.parseInt(SectionField.getText());
    DBCollection coll = db.getCollection("student");
    BasicDBObject andQuery = new BasicDBObject();
    List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
    obj.add(new BasicDBObject("firstname", stuFirst));
    obj.add(new BasicDBObject("mid_name", stuMid));
    obj.add(new BasicDBObject("lastname", stuLast));
    obj.add(new BasicDBObject("grade", grade));
    obj.add(new BasicDBObject("section", section));

    andQuery.put("$and", obj);
    DBCursor cursor = coll.find(andQuery);

    if (cursor.count() != 0) {
        UpdateBtn.setVisible(true);
        MidField.setVisible(true);
        FinalField.setVisible(true);
        jLabel6.setVisible(true);
        jLabel7.setVisible(true);
        jLabel8.setVisible(true);

        BasicDBList marks = (BasicDBList) cursor.next().get("marks");
        for (int j = 0; j < marks.size(); ++j) {
            BasicDBObject mark = (BasicDBObject) marks.get(j);

            if (mark.getString("teacher").equals(firstname)) {

                if (mark.get("mid") == null) {

                    MidField.setText("-1");
                    FinalField.setText("-1");

                } else if (mark.get("mid") != null && mark.get("final") == null) {

                    MidField.setText(mark.getString("mid"));
                    FinalField.setText("-1");
                } else {

                    MidField.setText(mark.getString("mid"));
                    FinalField.setText(mark.getString("final"));

                }

            }
        }
    } else {
        JOptionPane.showMessageDialog(null, "wrong data,try again!");
        FirstnameField.setText("");
        MidnameField.setText("");
        LastnameField.setText("");
        GradeField.setText("");
        SectionField.setText("");
    }
}

From source file:App.modules.main_menu.model.dao.Sign_in_DAO.java

public static int Select_Admin_dni_mongo() {
    DBCursor cursor = null;
    int ok = 0;//from w w  w  .  ja  v a2 s  . co  m
    Singleton_App.c = new Client();

    try {

        BasicDBObject query = new BasicDBObject();
        query.put("user", Sign_in.User_txt.getText());
        query.put("pass", Sign_in.Pass_txt.getText());

        // BasicDBObject searchById = new BasicDBObject();
        // searchById.append("dni", 1).append("_id",0);

        cursor = Singleton_App.collection.find(query);

        if (cursor.count() != 0) {

            while (cursor.hasNext()) {

                BasicDBObject document = (BasicDBObject) cursor.next();

                Singleton_App.c = Singleton_App.c.Client_to_DB(document);

                ok = 1;

            }
        } else {

            ok = 0;
            // JOptionPane.showMessageDialog(null, "Cliente no encontrado "+ ok);
            // System.out.println("NOT DATA"); 

        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    // JOptionPane.showMessageDialog(null, Singleton_App.c);
    return ok;

}

From source file:app.modules.SignIn.model.DAO_Login.java

/**
 * Compara el nombre de usuario con el de todos los clientes de la base de datos, cuando encuentra una coincidencia
 * compara la contrasea introducida con la de la base de datos, cuando ambos datos coinciden devuelve true y
 * rellena el perfil del usuario//from   w  w w  . j av  a2 s  .c  om
 * @return login True si se ha logrado iniciar sesin
 */
public static boolean SignInClient() {
    boolean login = false;
    DBCursor cursor = null;
    client c = new client();
    try {
        cursor = singleton_global.collection.find(new BasicDBObject().append("user", addusername.getText()));
        if (cursor.count() != 0) {
            for (int i = 0; i < cursor.size(); i++) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                c = c.BBDD_to_client(document);
                fecha aux = new fecha();
                if (c.getPass().equals(addpass.getText())) {
                    new client_controller(new clientnew_view(), 2).Iniciar(2);
                    adddnic.setText(c.getDni());
                    caddname.setText(c.getName());
                    caddsurname.setText(c.getSubname());
                    caddmobile.setText(c.getMobile());
                    caddemail.setText(c.getEmail());
                    cadddatebirthday.setCalendar(aux.stringtocalendar(c.getDate_birthday()));
                    caddnameuser.setText(c.getUser());
                    caddpassword.setText(c.getPass());
                    caddavatar.setText(c.getAvatar());
                    cadd_status.setSelectedItem(c.getState());
                    caddreg.setCalendar(aux.stringtocalendar(c.getUp_date()));
                    caddshopping.setText(Float.toString(c.getShopping()));
                    caddpremium.setSelectedItem(c.getPremium());
                    caddtype.setText(c.getClient_type());
                    login = true;
                    if ("Premium".equals(c.getPremium())) {
                        cadddesc.setText("10%");
                    } else {
                        cadddesc.setText("0%");
                    }
                    caddyearsservice.setText(Integer.toString(
                            aux.restafechas(aux.stringtocalendar(c.getUp_date()), aux.fechasystem(), "years")));
                }
            }
        } else {
            System.out.println("NOT DATA");
        }
    } catch (Exception e) {
        if (cursor != null) {
            cursor.close();
        }
    }
    return login;
}

From source file:App.modules.users.Client.Model.dao.Client_DB_DAO.java

/**
 * //ww w  .  j  a v a 2s  .c om
 */

public static void print_table() {
    DBCursor cursor = null;
    // String rdo = "";
    Client c = new Client();

    Singleton_cli.cli.clear();
    try {
        cursor = Singleton_App.collection.find();
        if (cursor.count() != 0) {

            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();

                // JOptionPane.showMessageDialog(null, c.toString());
                Singleton_cli.cli.add(c.Client_to_DB(document));
                // JOptionPane.showMessageDialog(null, Singleton_cli.cli.toString());
                // rdo = rdo + (c.getNombre() + " - " + w.getApellidos() + " - " +w.getEdad() + "\n");
            }
        } else {
            System.out.println("NOT DATA");
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    //return rdo;   
}

From source file:app.modules.users.client.model.DAO.DAO_Mongo.java

/**
 * Lee todos los clientes de la bbdd para mostrarlos en nuestra app
 *//*w w w  .  j  av a2  s .  c o  m*/
public static void read_client() {
    DBCursor cursor = null;
    client c = new client();
    try {
        cursor = singleton_global.collection.find();
        if (cursor.count() != 0) {
            for (int i = 0; i < cursor.size(); i++) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                c = c.BBDD_to_client(document);
                client j = new client(c.getDni(), c.getName(), c.getSubname(), c.getMobile(), c.getEmail(),
                        c.getDate_birthday(), c.getUser(), c.getPass(), c.getAvatar(), c.getState(),
                        c.getUp_date(), c.getShopping(), c.getPremium(), c.getClient_type());
                singleton_client.client.AddClient(j);
            }
        } else {
            System.out.println("NOT DATA");
        }
    } catch (Exception e) {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:applango.common.services.DB.mongo.mongoDB.java

public static DBObject getRollupValue(DBCollection coll, String userId) {
    logger.info("Getting today's rollup record for user " + userId);
    DBCursor cursor;
    int maxWait = 10;
    try {/*from   ww w  .j ava2  s  . c  o m*/
        DateTime jsonToday = new DateTime();
        DateTime yesterday = new DateTime().minusDays(1);

        Date yesterdayDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(yesterday.toString());
        BasicDBObject dateQueryObj = new BasicDBObject("beginDate", new BasicDBObject("$gt", yesterdayDate));
        dateQueryObj.append("userId", userId);
        dateQueryObj.append("customerId", "automationCustomer");
        dateQueryObj.append("appName", "salesforce");

        cursor = coll.find(dateQueryObj);

        while ((cursor.count() == 0) && maxWait > 0) {
            sleep(1000);
            maxWait--;
        }

    } catch (Exception ex) {
        cursor = null;
        logger.error(ex.getMessage());
    }
    return cursor.next();
}

From source file:backend.facades.UserController.java

@Override
public Integer getMembersCount() {
    Integer listCount = 0;//from  w  w w  .ja  v a 2 s . co m
    try {
        BasicDBObject query = new BasicDBObject();
        DBCursor cursor = userCollection.find(query);
        listCount = cursor.count();
        System.out.println("MEMBER COUNT backend" + listCount);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return listCount;
}

From source file:backend.facades.UserController.java

public UserEntity getUserByEmail(String email) {
    UserEntity userEntity = null;//from   w  w  w. ja  v a 2s  .c om
    BasicDBObject query = new BasicDBObject();
    query.put("email", email);
    DBCursor cursor = userCollection.find(query);
    try {
        if (cursor.count() > 0) {
            DBObject document = cursor.next();
            userEntity = new UserEntity();
            userEntity.setId((Long) document.get("_id"));
            userEntity.setUsername((String) document.get("username"));
            userEntity.setEmail((String) document.get("email"));
            userEntity.setFirstname((String) document.get("firstname"));
            userEntity.setPasswd((String) document.get("passwd"));
            userEntity.setLastname((String) document.get("lastname"));
            userEntity.setStatus((Integer) document.get("status"));
            userEntity.setImageId((String) document.get("imageId"));
            userEntity.setRegisteredDate((Date) document.get("registeredDate"));
            userEntity.setLastLoginDate((Date) document.get("lastLoginDate"));
            userEntity.setLanguageCode((String) document.get("languageCode"));
            userEntity.setUserRole((Integer) document.get("userRole"));
            userEntity.setModeratorValue((Integer) document.get("moderator"));
            userEntity.setPersonalWebPage((String) document.get("webpage"));
            Integer value = (Integer) document.get("subscribe");
            if (value != null && value.equals(StatusTypes.ACCEPT_LICENSE)) {
                userEntity.setAcceptSubscr(true);
            } else {
                userEntity.setAcceptSubscr(false);
            }
        }
    } finally {
        cursor.close();
    }
    return userEntity;
}

From source file:backend.facades.UserController.java

public UserEntity getUserById(Long id) {
    UserEntity userEntity = null;/*from ww  w.  j ava2 s . c o  m*/
    BasicDBObject query = new BasicDBObject();
    query.put("_id", id);
    DBCursor cursor = userCollection.find(query);
    try {
        if (cursor.count() > 0) {
            DBObject document = cursor.next();
            userEntity = new UserEntity();
            userEntity.setId((Long) document.get("_id"));
            userEntity.setUsername((String) document.get("username"));
            userEntity.setEmail((String) document.get("email"));
            userEntity.setFirstname((String) document.get("firstname"));
            userEntity.setPasswd((String) document.get("passwd"));
            userEntity.setLastname((String) document.get("lastname"));
            userEntity.setStatus((Integer) document.get("status"));
            userEntity.setImageId((String) document.get("imageId"));
            userEntity.setRegisteredDate((Date) document.get("registeredDate"));
            userEntity.setLastLoginDate((Date) document.get("lastLoginDate"));
            userEntity.setLanguageCode((String) document.get("languageCode"));
            userEntity.setUserRole((Integer) document.get("userRole"));
            userEntity.setModeratorValue((Integer) document.get("moderator"));
            Integer value = (Integer) document.get("subscribe");
            userEntity.setPersonalWebPage((String) document.get("webpage"));
            if (value != null && value.equals(StatusTypes.ACCEPT_SUBSCRB)) {
                userEntity.setAcceptSubscr(true);
            } else {
                userEntity.setAcceptSubscr(false);
            }
        }
    } finally {
        cursor.close();
    }
    return userEntity;
}

From source file:backend.facades.UserController.java

public Long getUserIdByEmail(String email) {
    Long id = 0L;//from w  w  w  . j  a  v a 2s  .  co  m
    BasicDBObject query = new BasicDBObject();
    query.put("email", email);
    DBCursor cursor = userCollection.find(query);
    try {
        if (cursor.count() > 0) {
            DBObject document = cursor.next();
            id = (Long) document.get("_id");
        }
    } finally {
        cursor.close();
    }
    return id;
}