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:fr.eolya.crawler.queue.mongodb.MongoDBSourceItemsQueue.java

License:Apache License

/** 
 * Check if item is in queue/* w  w w  . j  a  v a  2 s .co  m*/
 * 
 * @return requested field value or null
 */
//private String contains(String keyValue, String returnedField) {   
//   BasicDBObject doc = getInternal(keyValue, false);
//   if (doc==null) return null;
//   return doc.getString(returnedField);         
//}

private BasicDBObject getInternal(String keyValue, boolean done) {

    String queryTimeStamp;
    if (done) {
        if (startDepth == 0)
            queryTimeStamp = "{\"" + timestampFieldName + "\":" + String.valueOf(startTime) + "}";
        else {
            queryTimeStamp = "{\"" + timestampFieldName + "\": {\"$lte\": " + String.valueOf(startTime) + "}}";
        }
    } else
        queryTimeStamp = "{\"" + timestampFieldName + "\": {\"$gt\": " + String.valueOf(startTime) + "}}";

    String queryHash = "{\"" + hashFieldName + "\":" + keyValue.hashCode() + "}";
    String query = "{\"$and\": [" + queryTimeStamp + ", " + queryHash + "]}";

    BasicDBObject docsearch = MongoDBHelper.JSON2BasicDBObject(query);

    //synchronized (collMonitor) {
    DBCursor cur = coll.getColl().find(docsearch);
    if (cur.count() == 0)
        return null;
    while (cur.hasNext()) {
        BasicDBObject doc = (BasicDBObject) cur.next();
        if (keyValue.equals(doc.getString(uniqueKeyFieldName))) {
            return doc;
        }
    }
    //}
    return null;
}

From source file:fr.eolya.utils.nosql.mongodb.MongoDBCollection.java

License:Apache License

public ArrayList<String> getValues(BasicDBObject docsearch, String field) {
    DBCursor cur = null;
    if (docsearch != null)
        cur = coll.find(docsearch);//from ww w  .j a  va 2s . com
    else
        cur = coll.find();
    if (cur.count() == 0)
        return null;
    ArrayList<String> values = new ArrayList<String>();
    while (cur.hasNext()) {
        BasicDBObject doc = (BasicDBObject) cur.next();
        values.add((String) doc.getString(field));
    }
    return values;
}

From source file:fr.eolya.utils.nosql.mongodb.MongoDBCollection.java

License:Apache License

public String getValue(BasicDBObject docsearch, String field) {
    DBCursor cur = coll.find(docsearch);
    if (cur.count() != 1)
        return null;
    BasicDBObject doc = (BasicDBObject) cur.next();
    return (String) doc.getString(field);
}

From source file:fr.eolya.utils.nosql.mongodb.MongoDBCollection.java

License:Apache License

public String getFirstValue(BasicDBObject docsearch, String field) {
    DBCursor cur = coll.find(docsearch);
    if (cur.count() == 0)
        return null;
    BasicDBObject doc = (BasicDBObject) cur.next();
    return (String) doc.getString(field);
}

From source file:fr.eolya.utils.nosql.mongodb.MongoDBCollection.java

License:Apache License

public BasicDBObject get(BasicDBObject docsearch) {
    DBCursor cur = coll.find(docsearch);
    if (cur.count() != 1)
        return null;
    BasicDBObject doc = (BasicDBObject) cur.next();
    return doc;/* ww w.j a  v a2  s.  c  o m*/
}

From source file:framework.mod.settings.model.DAO.DAO_login.java

/**Search on mongo client collection for a client with usernam and password 
 * gived on main login//w  ww.  j a  va 2  s. c  om
 * 
 * @return boolean
 */
public static boolean DAO_searchONclient() {
    boolean valid = false;
    DBCursor cursor = null;
    Client clt = new Client();

    try {
        BasicDBObject query = new BasicDBObject();
        query.put("user", singletonProfile.userName);

        cursor = singletonGen.collection.find(query);
        if (cursor.count() != 0) {
            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                clt = singletonProfile.clt.DB_to_Client(document);

                if (clt.getPassword().equals(singletonProfile.userPass)) {
                    valid = true;
                    System.out.println("ERROR Login for while!!!");
                }
            }

        } else {
            System.out.println("NOT DATA");
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return valid;
}

From source file:framework.mod.user.client.model.DAO.DAO_DB_Client.java

/**LOAD MONGO CLIENT DB
 * /*from w  w w . j  av  a  2  s . co  m*/
 * Function that count the elements on singletonGen.table. 
 * Reads his content one by one, saving client on document, 
 * transform and saves it on singletonClient.ephemeralClient.
 * Finally adds singletonClient.ephemeralClient on singletonClient.ClienTableArray
 * 
 * @return void
 */
public static void mongoDB_load_clientAL() {
    DBCursor index = null;

    try {
        index = singletonGen.collection.find();
        if (index.count() != 0) {
            while (index.hasNext()) {
                BasicDBObject document = (BasicDBObject) index.next();
                //singletonClient.ephemeralClient = singletonClient.ephemeralClient.DB_to_Client(document);
                singletonClient.ClienTableArray.add(singletonClient.ephemeralClient.DB_to_Client(document));
            }
        } else {
            System.out.println("NOT DATA");
        }
    } finally {
        if (index != null) {
            index.close();
        }
    }
}

From source file:framework.mod.user.client.model.DAO.DAO_DB_Client.java

/**Search on mongo client collection for a client with usernam gived
 * //  www.ja  v  a  2s  .  co  m
 * @return boolean
 */
public static boolean DAO_searchONclientBYuserName() {
    boolean valid = false;
    DBCursor cursor = null;
    Client clt = new Client();

    try {
        BasicDBObject query = new BasicDBObject();
        query.put("user", singletonClient.userName);

        cursor = singletonGen.collection.find(query);
        if (cursor.count() != 0) {
            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                clt = singletonClient.ephemeralClient.DB_to_Client(document);

                if (clt.getPassword().equals(singletonClient.ephemeralClient)) {
                    valid = true;
                    System.out.println("ERROR Login for while!!!");
                }
            }

        } else {
            System.out.println("NOT DATA");
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return valid;
}

From source file:framework.module.users.client.model.DAO.DAO_BD_client.java

/**
 * BD TO ARRAYLIST/*from   w  w  w.  j av  a 2  s.co  m*/
 */
public static void BDtoArray() {
    DBCursor cursor = null;
    String rdo = "";
    Client c1 = new Client();
    try {
        cursor = collection.find().sort(new BasicDBObject("DNI", -1));
        if (cursor.count() != 0) {
            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                c1 = c1.BD_to_Client(document);
                Singleton_client.userclient.add(c1);
            }
        } else {
            System.out.println("NOT DATA");
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:framework.module.users.client.model.DAO.DAO_BD_client.java

/**
 * FIND USER//from  w  ww  .jav  a 2s .c om
 * @param dni 
 */
public static void find_BD(String dni) {
    DBCursor cursor = null;
    String rdo = "";
    try {
        BasicDBObject query = new BasicDBObject("DNI", dni);
        cursor = collection.find(query);
        if (cursor.count() != 0) {
            Singleton_client.client = new Client();
            while (cursor.hasNext()) {
                BasicDBObject document = (BasicDBObject) cursor.next();
                Singleton_client.client = Singleton_client.client.BD_to_Client(document);
            }
        } else {
            Singleton_client.client = null;
            System.out.println("NOT DATA");
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}