Example usage for com.mongodb DBCollection find

List of usage examples for com.mongodb DBCollection find

Introduction

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

Prototype

public DBCursor find(final DBObject query) 

Source Link

Document

Select documents in collection and get a cursor to the selected documents.

Usage

From source file:com.servlet.YearExpense.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from   w w  w  .jav  a2  s  .c om*/
 * @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 {

    PrintWriter out = response.getWriter();
    DBcon dbcon = new DBcon();
    dbcon.getDbCon();
    JSONObject json = new JSONObject();
    DBCollection coll = dbcon.getData("claims");

    DBCursor cursor = coll.find(new BasicDBObject("status", "Approved"));
    int foodSum = 0;
    int internetSum = 0;
    int otherSum = 0;
    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        ArrayList li = (ArrayList) obj.get("details");

        if (!li.isEmpty()) {

            Iterator<BasicDBObject> intI = li.iterator();
            while (intI.hasNext()) {
                BasicDBObject o = intI.next();
                Claims cl = Claims.fromDBObject(o);
                String billType = (String) cl.getBillType();
                if (billType.equals("Food")) {
                    foodSum += (int) cl.getAmount();

                } else if (billType.equals("Internet")) {
                    internetSum += (int) cl.getAmount();
                } else {
                    otherSum += (int) cl.getAmount();
                }

            }

        }
    }
    json.put("foodsum", foodSum);
    json.put("internetsum", internetSum);
    json.put("othersum", otherSum);
    out.print(json);

}

From source file:com.sfelf.connectors.mongoOplogCursorConnector.java

License:Open Source License

/**
 * Returns {@link DBCursor} for the specified oplog collection with the specified query applied. If fullDocument
 * is false then only the fields specified by {@link #getOplogFields() getOplogFields()} will be returned.
 * /*w w w . j  a  va 2s  .c o m*/
 * @param oplog       {@link DBCollection} specifying the collection to use when creating the cursor
 * @param query       {@link DBObject} specifying the query to use when creating the cursor
 * @param fullDocument    {@link Boolean} specifying if the full document should be returned or the fields specified by {@link #getOplogFields() getOplogFields()}
 * @return {@link DBCursor}
 */
private DBCursor createCursor(DBCollection oplog, DBObject query, Boolean fullDocument) {
    DBCursor cursor;

    if (fullDocument) {
        cursor = oplog.find(query);
    } else {
        DBObject fields = getOplogFields();
        cursor = oplog.find(query, fields);
    }
    return cursor.addOption(Bytes.QUERYOPTION_TAILABLE).addOption(Bytes.QUERYOPTION_AWAITDATA);
}

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 {/*from   w  w  w.  j ava 2s . 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 .  java 2 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  ww w .  j a  v  a 2 s . c o m*/
 * 
 * @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 w w w  . j a  va  2  s. 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.sitewhere.mongodb.MongoPersistence.java

License:Open Source License

/**
 * List all items in the collection that match the qiven query.
 * /*from   www  . jav a2 s  . c o  m*/
 * @param api
 * @param collection
 * @param query
 * @param sort
 * @return
 */
public static <T> List<T> list(Class<T> api, DBCollection collection, DBObject query, DBObject sort) {
    DBCursor cursor = collection.find(query);
    List<T> matches = new ArrayList<T>();
    MongoConverter<T> converter = MongoConverters.getConverterFor(api);
    try {
        while (cursor.hasNext()) {
            DBObject match = cursor.next();
            matches.add(converter.convert(match));
        }
    } finally {
        cursor.close();
    }
    return matches;
}

From source file:com.sitewhere.mongodb.user.MongoUserManagement.java

License:Open Source License

public List<IUser> listUsers(IUserSearchCriteria criteria) throws SiteWhereException {
    DBCollection users = getMongoClient().getUsersCollection();
    DBObject dbCriteria = new BasicDBObject();
    if (!criteria.isIncludeDeleted()) {
        MongoSiteWhereEntity.setDeleted(dbCriteria, false);
    }//from www .  j av  a 2  s .c  o  m
    DBCursor cursor = users.find(dbCriteria).sort(new BasicDBObject(MongoUser.PROP_USERNAME, 1));
    List<IUser> matches = new ArrayList<IUser>();
    try {
        while (cursor.hasNext()) {
            DBObject match = cursor.next();
            matches.add(MongoUser.fromDBObject(match));
        }
    } finally {
        cursor.close();
    }
    return matches;
}

From source file:com.smartlearner.email.analytics.dbmanage.java

public static void prnt(Pattern nr, DBCollection col) {

    BasicDBObject query = new BasicDBObject("date", nr);

    DBCursor cursor = col.find(query);
    System.out.println("***** Output *****");
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
    }//from w  w w .  j  av a  2 s . c om

}

From source file:com.smbtec.xo.mongodb.impl.JSONQuery.java

License:Apache License

private ResultIterator<Map<String, Object>> execute0(String json, Class<?> type,
        Map<String, Object> parameters) {
    // TODO/*from  w  w  w . j a v a2  s.c  om*/
    DBCollection collection = database.getCollection("A");

    final DBCursor matches = collection.find((DBObject) JSON.parse(json));

    return new ResultIterator<Map<String, Object>>() {

        public boolean hasNext() {
            return matches.hasNext();
        }

        public Map<String, Object> next() {
            DBObject next = matches.next();
            final Map<String, Object> result = new HashMap<>();
            // TODO
            result.put("result", new MongoDbDocument(next, "A"));
            return result;
        }

        public void remove() {
            throw new XOException("Remove operation is not supported for query results.");
        }

        public void close() {
            matches.close();
        }
    };

}