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:com.seyren.mongo.MongoStore.java

License:Apache License

@Override
public SeyrenResponse<Alert> getAlerts(int start, int items) {
    DBCursor dbc = getAlertsCollection().find().sort(object("timestamp", -1)).skip(start).limit(items);
    List<Alert> alerts = new ArrayList<Alert>();
    while (dbc.hasNext()) {
        alerts.add(mapper.alertFrom(dbc.next()));
    }/*www.  j av  a2 s  .co  m*/
    dbc.close();
    return new SeyrenResponse<Alert>().withValues(alerts).withItems(items).withStart(start)
            .withTotal(dbc.count());
}

From source file:com.sitewhere.mongodb.device.MongoDeviceEventManagement.java

License:Open Source License

@Override
public ISearchResults<IDeviceEvent> listDeviceEvents(String assignmentToken, IDateRangeSearchCriteria criteria)
        throws SiteWhereException {
    DBCollection events = getMongoClient().getEventsCollection(getTenant());
    BasicDBObject query = new BasicDBObject(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_TOKEN, assignmentToken);
    MongoPersistence.addDateSearchCriteria(query, MongoDeviceEvent.PROP_EVENT_DATE, criteria);
    BasicDBObject sort = new BasicDBObject(MongoDeviceEvent.PROP_EVENT_DATE, -1)
            .append(MongoDeviceEvent.PROP_RECEIVED_DATE, -1);

    int offset = Math.max(0, criteria.getPageNumber() - 1) * criteria.getPageSize();
    DBCursor cursor = events.find(query).skip(offset).limit(criteria.getPageSize()).sort(sort);
    List<IDeviceEvent> matches = new ArrayList<IDeviceEvent>();
    SearchResults<IDeviceEvent> results = new SearchResults<IDeviceEvent>(matches);
    try {/* www . j av  a2  s  . c o  m*/
        results.setNumResults(cursor.count());
        while (cursor.hasNext()) {
            DBObject match = cursor.next();
            matches.add(MongoPersistence.unmarshalEvent(match));
        }
    } finally {
        cursor.close();
    }
    return results;
}

From source file:com.sitewhere.mongodb.device.MongoDeviceManagement.java

License:Open Source License

@Override
public ISearchResults<IDeviceEvent> listDeviceEvents(String assignmentToken, IDateRangeSearchCriteria criteria)
        throws SiteWhereException {
    DBCollection events = getMongoClient().getEventsCollection();
    BasicDBObject query = new BasicDBObject(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_TOKEN, assignmentToken);
    MongoPersistence.addDateSearchCriteria(query, MongoDeviceEvent.PROP_EVENT_DATE, criteria);
    BasicDBObject sort = new BasicDBObject(MongoDeviceEvent.PROP_EVENT_DATE, -1)
            .append(MongoDeviceEvent.PROP_RECEIVED_DATE, -1);

    int offset = Math.max(0, criteria.getPageNumber() - 1) * criteria.getPageSize();
    DBCursor cursor = events.find(query).skip(offset).limit(criteria.getPageSize()).sort(sort);
    List<IDeviceEvent> matches = new ArrayList<IDeviceEvent>();
    SearchResults<IDeviceEvent> results = new SearchResults<IDeviceEvent>(matches);
    try {//from   ww  w  .  ja  v  a2 s . co  m
        results.setNumResults(cursor.count());
        while (cursor.hasNext()) {
            DBObject match = cursor.next();
            matches.add(MongoPersistence.unmarshalEvent(match));
        }
    } finally {
        cursor.close();
    }
    return results;
}

From source file:com.sitewhere.mongodb.MongoPersistence.java

License:Open Source License

/**
 * Search the given collection using the provided query and sort. Return the paged
 * seaerch results.//from  w w  w. ja v  a2 s .  com
 * 
 * @param api
 * @param collection
 * @param query
 * @param sort
 * @param pageNumber
 * @param pageSize
 * @return
 */
public static <T> SearchResults<T> search(Class<T> api, DBCollection collection, DBObject query, DBObject sort,
        ISearchCriteria criteria) {
    int offset = Math.max(0, criteria.getPageNumber() - 1) * criteria.getPageSize();
    DBCursor cursor = collection.find(query).skip(offset).limit(criteria.getPageSize()).sort(sort);
    List<T> matches = new ArrayList<T>();
    SearchResults<T> results = new SearchResults<T>(matches);
    MongoConverter<T> converter = MongoConverters.getConverterFor(api);
    try {
        results.setNumResults(cursor.count());
        while (cursor.hasNext()) {
            DBObject match = cursor.next();
            matches.add(converter.convert(match));
        }
    } finally {
        cursor.close();
    }
    return results;
}

From source file:com.sitewhere.mongodb.MongoPersistence.java

License:Open Source License

/**
 * Search the given collection using the provided query and sort.
 * /*from   ww  w .ja  v a2s  .  c o m*/
 * @param api
 * @param collection
 * @param query
 * @param sort
 * @return
 */
public static <T> SearchResults<T> search(Class<T> api, DBCollection collection, DBObject query,
        DBObject sort) {
    DBCursor cursor = collection.find(query).sort(sort);
    List<T> matches = new ArrayList<T>();
    SearchResults<T> results = new SearchResults<T>(matches);
    MongoConverter<T> converter = MongoConverters.getConverterFor(api);
    try {
        results.setNumResults(cursor.count());
        while (cursor.hasNext()) {
            DBObject match = cursor.next();
            matches.add(converter.convert(match));
        }
    } finally {
        cursor.close();
    }
    return results;
}

From source file:com.softlyinspired.jlw.concerns.concern.java

License:Open Source License

/**
 * Checks if the concernId already exists and returns boolean
 *///from w  ww . ja  v  a  2 s. c o  m
public boolean checkExistingId(int CheckId) {

    try {
        /* Get the database collection */
        concernColl = repoConnection.getConcernCollection();
        if (concernColl == null) {
            return false;
        }
        ;

        // search for the id
        BasicDBObject query = new BasicDBObject();
        query.put("concernId", CheckId);
        DBCursor checkDoc = concernColl.find(query).limit(1);

        if (checkDoc.count() == 0) {
            concernExists = false;
        } else {
            currentConcernDoc = checkDoc.next();
            concernExists = true;
        }

    } catch (Exception e) {
        System.out.println("error finding concern");
        concernExists = false;
    }
    return concernExists;
}

From source file:com.softlyinspired.jlw.concerns.concernSet.java

License:Open Source License

public String[][] listall() {
    String concernText = new String();
    String concernList[][] = new String[100][4];

    try {//  www  .j  ava 2s  .  c o m

        DBCollection coll = repoConnection.getConcernCollection();
        DBObject doc;

        String[] scriptHeader = new String[3];
        try {
            DBCursor allConcerns = coll.find();
            concernCount = allConcerns.count();

            for (int i = 0; i < (concernCount); i++) {
                doc = allConcerns.next();
                scriptHeader = getDetails(doc);
                concernList[i][0] = scriptHeader[0];
                concernList[i][1] = scriptHeader[1];
                concernList[i][2] = scriptHeader[2];
                concernList[i][3] = scriptHeader[3];
            }

        } catch (Exception e) {
            concernText = "No concerns have yet been defined " + coll.getFullName();
            JOptionPane.showMessageDialog(null, concernText, "Error", JOptionPane.PLAIN_MESSAGE);
        }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.PLAIN_MESSAGE);

    }
    return concernList;

}

From source file:com.softlyinspired.jlw.connections.ConnectionSelector.java

License:Open Source License

private ArrayList<dbConnection> allConnections() {
    int numberFound = 0;
    ArrayList<dbConnection> connectionList = new ArrayList<dbConnection>();
    try {//from   w ww  .  ja  va2s .  com

        DBCollection coll = repoConnection.getConnectionCollection();
        DBObject doc;

        try {
            DBCursor allConnections = coll.find();
            numberFound = allConnections.count();

            for (int i = 0; i < (numberFound); i++) {
                doc = allConnections.next();
                dbConnection a = new dbConnection();
                a.connectionName = doc.get("name").toString();
                connectionList.add(a);
            }
        } catch (Exception e) {
            JLWUtilities.scriptErrorMessage("Error finding connection");
        }
    } catch (Exception e) {
        JLWUtilities.scriptErrorMessage("General Error in listing");
    }

    return connectionList;
}

From source file:com.softlyinspired.jlw.connections.dbConnection.java

License:Open Source License

public boolean checkExisting() {
    /**/*  w  ww.  j a v a2s  .  c o  m*/
     * sets value of concernExists;
     */
    boolean nameExists;
    try {
        /* Get the database collection */
        DBCollection connectionColl = repoConnection.getConnectionCollection();
        if (connectionColl == null) {
            return false;
        }
        ;
        // search for the id
        BasicDBObject query = new BasicDBObject();
        query.put("name", connectionName);

        DBCursor checkDoc = connectionColl.find(query).limit(1);

        if (checkDoc.count() == 0) {
            nameExists = false;
        } else {
            nameExists = true;
            currentConnectionDoc = checkDoc.next();
        }

    } catch (Exception e) {
        System.out.println("Connection does not exist");
        nameExists = false;
    }
    return nameExists;
}

From source file:com.softlyinspired.jlw.mongodb.IdManager.java

License:Open Source License

public int getNextId(String collectionType) {
    String n = new String();
    DBCollection coll;// w w  w . j  a  v a  2 s.co  m
    String collkey = new String();

    switch (collectionType) {
    case "concern":
        coll = repoConnection.getConcernCollection();
        collkey = "concernId";
        break;
    case "script":
        coll = repoConnection.getScriptCollection();
        collkey = "scriptId";
        break;
    case "connection":
        coll = repoConnection.getConnectionCollection();
        collkey = "connectionId";
        break;
    case "menu":
        coll = repoConnection.getCustomMenusCollection();
        collkey = "scriptId";
        break;
    case "report":
        coll = repoConnection.getReportsCollection();
        collkey = "reportId";
        break;
    default:
        coll = null;
        break;
    }

    DBObject sort = new BasicDBObject();
    // -1 sorts descending
    sort.put(collkey, -1);

    DBCursor obj = coll.find().sort(sort).limit(1);

    System.out.println(obj.count());
    if (obj.count() > 0) {
        DBObject doc;
        doc = obj.next();
        n = doc.get(collkey).toString();
        return Integer.parseInt(n);
    } else {
        return 0;
    }

}