List of usage examples for com.mongodb DBCursor toArray
public List<DBObject> toArray()
From source file:models.UserGroup.java
License:Open Source License
public static List<UserGroup> listGroupsOfUser(ObjectId uid) { List<UserGroup> u = null; try {//w w w .j a va 2s . co m DBCursor iobj = (DBCursor) MongoDB.getDB().getCollection(MongoDB.CUserGroup).find(); if (iobj != null) u = Lists.transform(iobj.toArray(), MongoDB.getSelf().toUserGroup()); } catch (Exception ex) { Logger.info("user load fail"); ex.printStackTrace(); Logger.info(ex.toString()); return null; } return u; }
From source file:models.UserGroup.java
License:Open Source License
public static List<UserGroup> loadGroups() { List<UserGroup> u = null; try {/*from ww w . j av a 2s . c o m*/ DBCursor iobj = (DBCursor) MongoDB.getDB().getCollection(MongoDB.CUserGroup).find(); if (iobj != null) u = Lists.transform(iobj.toArray(), MongoDB.getSelf().toUserGroup()); } catch (Exception ex) { Logger.info("user load fail"); ex.printStackTrace(); Logger.info(ex.toString()); return null; } return u; }
From source file:models.UserLocation.java
License:Open Source License
public static List<UserLocation> getUserLocationHistory(String uid, Integer start, Integer count) { List<UserLocation> r = null; try {/*w ww. ja v a 2s .c o m*/ BasicDBObject query = new BasicDBObject().append("userid", uid); BasicDBObject sort = new BasicDBObject().append("time", -1); // TODO natural sort DBCursor iobj = MongoDB.getDB().getCollection(MongoDB.CUserLocation).find(query).sort(sort).skip(start) .limit(count); if (iobj == null) { r = new ArrayList<UserLocation>(); } else { Logger.info("user threads found"); r = Lists.transform(iobj.toArray(), MongoDB.getSelf().toUserLocation()); } } catch (Exception ex) { Logger.info("getUserThreads"); ex.printStackTrace(); Logger.info(ex.toString()); } return r; }
From source file:models.UserLocation.java
License:Open Source License
public static List<UserLocation> getLocationHistory(String uid, Integer start, Integer count) { List<UserLocation> r = null; try {// w ww .j a va2 s. com // hm.. toto moze byt vlastne staticke/singleton... BasicDBObject query = new BasicDBObject().append("location", uid); BasicDBObject sort = new BasicDBObject().append("time", -1); // TODO natural sort DBCursor iobj = MongoDB.getDB().getCollection(MongoDB.CUserLocation).find(query).sort(sort).skip(start) .limit(count); if (iobj == null) { r = new ArrayList<UserLocation>(); } else { Logger.info("user threads found"); r = Lists.transform(iobj.toArray(), MongoDB.getSelf().toUserLocation()); } } catch (Exception ex) { Logger.info("getUserThreads"); ex.printStackTrace(); Logger.info(ex.toString()); } return r; }
From source file:NexT.db.mongo.DataModel.java
License:GNU General Public License
/** * Attempt to retrieve records from the specified collection using the * given query. If no records were found, null is returned instead. * @param collection The collection to query * @param query A DBObject representing the query parameters. * @param from From offset./*from w w w . j a va2s. c o m*/ * @param limit Max. number of entries. * @return Null if no documents were found or an array of records. * @throws MongoException Thrown if the MongoWrapper has not been * initialized yet or if there was an error reading the objects from the db. */ public static DataModel[] getData(String collection, DBObject query, int from, int limit) throws MongoException { MongoWrapper wrapper = MongoWrapper.getInstance(); if (wrapper == null) throw new MongoException("MongoWrapper has not been initiated."); DBCollection col = wrapper.getCollection(collection); DBCursor cursor = col.find(query).skip(from); if (limit != -1) cursor = cursor.limit(limit); List<DBObject> modelsList; DataModel[] models; try { modelsList = cursor.toArray(); models = new DataModel[modelsList.size()]; for (int i = 0; i < models.length; i++) { models[i] = new DataModel(col, modelsList.get(i)); } } catch (Exception ex) { throw new MongoException("Error while retrieving Objects", ex); } finally { cursor.close(); } if (models.length == 0) return null; else return models; }
From source file:org.apache.camel.component.mongodb.MongoDbProducer.java
License:Apache License
protected void doFindAll(Exchange exchange) throws Exception { DBCollection dbCol = calculateCollection(exchange); // do not use getMandatoryBody, because if the body is empty we want to retrieve all objects in the collection DBObject query = null;// ww w. j av a 2s .c o m // do not run around looking for a type converter unless there is a need for it if (exchange.getIn().getBody() != null) { query = exchange.getIn().getBody(DBObject.class); } DBObject fieldFilter = exchange.getIn().getHeader(MongoDbConstants.FIELDS_FILTER, DBObject.class); // get the batch size and number to skip Integer batchSize = exchange.getIn().getHeader(MongoDbConstants.BATCH_SIZE, Integer.class); Integer numToSkip = exchange.getIn().getHeader(MongoDbConstants.NUM_TO_SKIP, Integer.class); Integer limit = exchange.getIn().getHeader(MongoDbConstants.LIMIT, Integer.class); DBObject sortBy = exchange.getIn().getHeader(MongoDbConstants.SORT_BY, DBObject.class); DBCursor ret = null; try { if (query == null && fieldFilter == null) { ret = dbCol.find(new BasicDBObject()); } else if (fieldFilter == null) { ret = dbCol.find(query); } else { ret = dbCol.find(query, fieldFilter); } if (sortBy != null) { ret.sort(sortBy); } if (batchSize != null) { ret.batchSize(batchSize.intValue()); } if (numToSkip != null) { ret.skip(numToSkip.intValue()); } if (limit != null) { ret.limit(limit.intValue()); } Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.findAll); resultMessage.setBody(ret.toArray()); resultMessage.setHeader(MongoDbConstants.RESULT_TOTAL_SIZE, ret.count()); resultMessage.setHeader(MongoDbConstants.RESULT_PAGE_SIZE, ret.size()); } catch (Exception e) { // rethrow the exception throw e; } finally { // make sure the cursor is closed if (ret != null) { ret.close(); } } }
From source file:org.baldeapi.v1.persistence.GenericDAO.java
License:Apache License
public List<DBObject> find(DBObject filter, DBObject sort, int skip, int limit) { DBCursor cursor = collection.find(filter).sort(sort).skip(skip).limit(limit); List<DBObject> result = null; try {/*from w ww. j a va 2s .c o m*/ result = cursor.toArray(); } finally { cursor.close(); } return result; }
From source file:org.baldeapi.v1.persistence.GenericDAO.java
License:Apache License
public List<DBObject> find(DBObject filter, DBObject projection, DBObject sort, int skip, int limit) { DBCursor cursor = collection.find(filter, projection).sort(sort).skip(skip).limit(limit); List<DBObject> result = null; try {// w ww . j av a 2s . c om result = cursor.toArray(); } finally { cursor.close(); } return result; }
From source file:org.forgerock.openidm.repo.mongodb.impl.query.Queries.java
License:Open Source License
protected List<DBObject> executeQuery(QueryInfo queryInfo, Map<String, Object> params, DBCollection collection) throws BadRequestException { List<DBObject> result = null; if (queryInfo.isGroupQuery()) { String resultKey = ""; List<String> list = queryInfo.getAggregationParams(); List<DBObject> dboList = new ArrayList<DBObject>(); DBObject firstParam = new BasicDBObject(); boolean first = true; for (String s : list) { DBObject query = resolveQuery(s, params); if (first) { firstParam = query;/*from w ww. j av a 2 s . c o m*/ first = !first; } else { dboList.add(query); } } AggregationOutput output = collection.aggregate(firstParam, (DBObject[]) dboList.toArray(new BasicDBObject[0])); if (output.results().iterator().hasNext()) { result = new ArrayList<DBObject>(); } for (DBObject obj : output.results()) { result.add(obj); } } else { String q = (queryInfo.getQuery() == null) ? "{}" : queryInfo.getQuery(); DBObject query = resolveQuery(q, params); String f = (queryInfo.getFileds() == null) ? "{}" : queryInfo.getFileds(); DBObject fields = resolveQuery(f, params); String s = (queryInfo.getSort() == null) ? "{}" : queryInfo.getSort(); DBObject sort = resolveQuery(s, params); DBCursor cur = null; try { cur = collection.find(query, fields).sort(sort); result = cur.toArray(); } catch (Exception ex) { throw new BadRequestException(ex.getMessage()); } finally { cur.close(); } } return result; }
From source file:org.javaee7.extra.mongo.PersonSessionBean.java
License:Open Source License
public List<Person> getPersons() { List<Person> persons = new ArrayList(); DBCursor cur = personCollection.find(); System.out.println("getPersons: Found " + cur.length() + " person(s)"); for (DBObject dbo : cur.toArray()) { persons.add(Person.fromDBObject(dbo)); }//from w ww .j av a 2 s .c om return persons; }