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:framework.modules.users.client.Model.DAO.DAO_client_MG.java

/**
 * To load the users by DB//ww w. ja  va 2  s . c  o  m
 * @param db
 * @param table 
 */
public static void load_clients() {

    DBCollection table = singleton.collection;
    DBCursor cursor = null;
    client_class client = new client_class();
    try {
        cursor = table.find();
        if (cursor.count() != 0) {
            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                singleton_client.client.add(client.DB_to_client(document));
            }
        } else {
            System.out.println("NOT DATA");
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:framework.modules.users.client.Model.DAO.DAO_client_MG.java

public static boolean search_client() {

    DBCollection table = singleton.collection;
    DBCursor cursor = null;
    client_class client = new client_class();
    boolean correct = false;
    try {//from  w w  w  .  ja va  2  s. c om

        BasicDBObject searchById = new BasicDBObject();
        searchById.put("dni", log_in.jt_dni.getText());
        //singleton_client.c=new client_class(log_in.jt_dni.getText());

        cursor = table.find(searchById);
        if (cursor.count() != 0) {
            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                singleton_client.c = client.DB_to_client(document);
                //System.out.println(singleton_client.c);
                correct = true;
                //singleton_client.c.add(client.DB_to_client(document));
            }
        } else {
            System.out.println("NOT DATA");
            correct = false;
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return correct;

}

From source file:getquantityvolume.GetQuantityVolume.java

/**
 * @param args the command line arguments
 *///w  w  w  .j  av  a2  s.  co m
public static void main(String[] args) {
    //lee los datos de conexion y genera un dbCollection
    System.out.println("leyendo...");
    ReadPropertiesForMongo readAndConect = new ReadPropertiesForMongo();

    System.out.println("..." + readAndConect.getFile().getAbsolutePath());
    DBCollection datosVisu = readAndConect.getCollection();
    //lee los nombres de los volumenes y los guarda en una lista
    System.out.println("leyendo volumenes");
    VolumenesList volumenes = new VolumenesList();
    // al iterar la lista...
    for (Volumen vol : volumenes.getVolumenes()) {
        //busca en mongo la coincidencia de volumen
        BasicDBObject query = new BasicDBObject("filePath", new BasicDBObject("$regex", vol.getNombre()));
        DBCursor cursor = datosVisu.find(query);
        //... y setea la cantidad
        vol.setCantidad(cursor.count());
        System.out.println("volumen: " + vol.getNombre() + "\t cantidad: " + vol.getCantidad());
    }
    readAndConect.close();
    //Escribe los resultados en un txt
    System.out.println("Escribiendo...");
    WriteResult writeResult = new WriteResult(volumenes.getVolumenes());
    System.out.println("Finaliz el proceso.");
    System.out.println("Resultados disponibles en" + "\n" + writeResult.getFile().getAbsolutePath());
}

From source file:github.macrohuang.orm.mongo.core.MongoDBTemplate.java

License:Apache License

/**
 * Query the total count by example with manual specify DBChooser.
 * /*from w w  w. j ava  2s.  co  m*/
 * @see MongoDBTemplate#findByExample(DBChooser, Object)
 * @param <T>
 * @param chooser
 * @param entry
 * @return
 * @throws MongoDataAccessException
 */
public <T> int getCountByExample(DBChooser chooser, T entry) throws MongoDataAccessException {
    Assert.assertNotNull(entry);
    Assert.assertNotNull(chooser);
    DBCursor dbCursor = getCollection(chooser).find(DBObjectUtil.convertPO2DBObject(entry));
    return dbCursor == null ? 0 : dbCursor.count();
}

From source file:gov.llnl.iscr.iris.LDAHandler.java

License:Open Source License

/**
 * sets the threshold value to be used for filtering "junk" topics
 * to a value obtained by thresholding the topic semantic coherence scores
 * at the percentile value given.// w  w  w.  ja  v  a 2  s.c om
 * @param thresholdPercentile
 * @return
 */
public LDAHandler setTopicThreshold(float thresholdPercentile) {

    DBCursor semcoCur = model.getSemcoValues();
    int semcoCount = semcoCur.count();
    int limit = (int) (thresholdPercentile * semcoCount) - 1;

    this.topicThreshold = (Double) semcoCur.toArray().get(limit).get("semco");
    return this;
}

From source file:guesslocation.MongoQuery.java

public static void main(String[] args) {

    {/*w ww. j  a  v a 2 s . co m*/

        try {

            // Connect to mongodb
            MongoClient mongo = new MongoClient("localhost", 27017);

            // get database
            // if database doesn't exists, mongodb will create it for you
            DB db = mongo.getDB("test");

            // get collection
            // if collection doesn't exists, mongodb will create it for you
            DBCollection collection = db.getCollection("twitter");
            DBCollection Outcollection = db.getCollection("user_tw");
            DBCursor cursor;
            BasicDBObject query;
            //------------------------------------
            // ( 1 ) collection.find() --> get all document
            cursor = collection.find();
            System.out.println("( 1 ) .find()");
            System.out.println("results --> " + cursor.count());

            try {
                BasicDBObject IDquery = new BasicDBObject(); //2015-05-12T15:15:31Z
                while (cursor.hasNext()) {
                    DBObject data = cursor.next();
                    Long v_user_Id = (Long) data.get("user_Id");
                    if (v_user_Id == null) {
                        continue;
                    }

                    IDquery.append("user_Id", v_user_Id);
                    DBCursor IDcursor = Outcollection.find(IDquery);
                    if (IDcursor.hasNext() == false) {
                        BasicDBObject basicObj = GetUserRecord(v_user_Id, data);
                        try {
                            Outcollection.insert(basicObj);
                        } catch (Exception e) {
                            System.err.println("error on insert " + v_user_Id);
                        }
                        basicObj = null;
                        Thread.sleep(100);
                        Outcollection.ensureIndex(new BasicDBObject("user_Id", 1),
                                new BasicDBObject("unique", true));
                    }
                    IDcursor.close();
                    IDquery.clear();
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(MongoQuery.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                cursor.close();
            }

            System.out.println("---------------------------------");
            System.exit(0);

        } catch (UnknownHostException ex) {
            Logger.getLogger(MongoQuery.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

From source file:japura.MonoBugs.MonoBugs.java

License:BSD License

public boolean list(CommandSender sender, String[] args) {

    int myPage = 1;
    if (args.length == CMD_ARGS) {
        try {//from w  ww . jav  a  2s  . co  m
            myPage = Integer.parseInt(args[1]);
        } catch (NumberFormatException e) {
            return false;
        }
    } else if (args.length > CMD_ARGS) {
        return false;
    }

    //query all of our user's bug reports
    BasicDBObject query = new BasicDBObject();
    query.put("user", sender.getName());
    DBCursor cursor = table.find(query);

    //if there are none to show..
    if (cursor.count() == 0) {
        sender.sendMessage("there are no reports to show");
        return true;
    }
    //otherwise, list them all together separated by newlines.
    String userReports = "";

    while (cursor.hasNext()) {
        DBObject element = cursor.next();
        userReports += "ID: " + element.get("bugID") + " | " + element.get("status") + " | "
                + element.get("issue") + " | date: " + element.get("createdDate");
        if (element.containsField("reason")) {
            userReports += " | reason: " + element.get("reason");
        }
        userReports += "\n";
    }

    //divy the report into pages and get the desired page
    ChatPage page = ChatPaginator.paginate(userReports, myPage);

    //send each line of our page to the user
    sender.sendMessage("Page " + page.getPageNumber() + " of " + page.getTotalPages() + " for reports:");
    for (String line : page.getLines()) {
        sender.sendMessage(line);
    }

    return true;
}

From source file:japura.Tribes.Tribe.java

License:BSD License

public Block[] getEmeralds() {
    if (emeraldCache != null)
        return emeraldCache;
    Tribes.log("rebuilding emerald table for " + name);
    BasicDBObject query = new BasicDBObject();
    query.put("tribe", name);
    DBCursor cursor = Tribes.getEmeraldTable().find(query);
    int size = cursor.count();
    Block[] blocks = new Block[size];
    double x;/*ww  w.  j  a v a2s .com*/
    double y;
    double z;
    World world;
    Location loc;
    BasicDBObject current;
    for (int i = 0; i < size; i++) {
        if (!cursor.hasNext())
            break;
        current = ((BasicDBObject) cursor.next());
        x = current.getLong("X");
        y = current.getLong("Y");
        z = current.getLong("Z");
        world = Bukkit.getWorld(current.getString("world"));
        loc = new Location(world, x, y, z);
        blocks[i] = loc.getBlock();
    }

    emeraldCache = blocks;
    return blocks;
}

From source file:japura.Tribes.Tribe.java

License:BSD License

public Block[] getDiamonds() {

    if (diamondCache != null)
        return diamondCache;
    Tribes.log("rebuilding diamond table for " + name);
    BasicDBObject query = new BasicDBObject();
    query.put("tribe", name);
    DBCursor cursor = Tribes.getDiamondTable().find(query);
    int size = cursor.count();
    Block[] blocks = new Block[size];
    double x;/* w w  w .ja  v  a  2  s.  c om*/
    double y;
    double z;
    World world;
    Location loc;
    BasicDBObject current;
    for (int i = 0; i < size; i++) {
        current = ((BasicDBObject) cursor.next());
        x = current.getLong("X");
        y = current.getLong("Y");
        z = current.getLong("Z");
        world = Bukkit.getWorld(current.getString("world"));
        loc = new Location(world, x, y, z);
        blocks[i] = loc.getBlock();
    }

    diamondCache = blocks;
    return blocks;
}

From source file:japura.Tribes.Tribe.java

License:BSD License

public void addDiamond(Block em, Player user) {
    if (em.getType() != Material.DIAMOND_BLOCK)
        return;/*from  w ww .j  a  v a2s.c  o  m*/
    //diamonds.add(em);
    BasicDBObject query = new BasicDBObject();
    query.put("tribe", name);
    DBCursor cursor = Tribes.getPlugin().getDiamondTable().find(query);

    String name = "diamond_" + cursor.count();
    TeleportData data = new TeleportData(em, name, this);
    data.addAllowed(this); //TODO why is this needed?
    user.sendMessage("created new teleporter named " + name);
    diamondCache = null;
}