List of usage examples for com.mongodb DBCursor sort
public DBCursor sort(final DBObject orderBy)
From source file:it.sayservice.platform.smartplanner.otp.OTPStorage.java
License:Apache License
public List<Object> getObjectsByField(MongoTemplate template, String key, String value, String collectionName, Class destinationClass, String orderBy) { DBCollection collection = template.getCollection(collectionName); List<Object> result = new ArrayList<Object>(); QueryBuilder qb = QueryBuilder.start(key).is(value); DBCursor cursor = collection.find(qb.get()); if (orderBy != null) { BasicDBObject sb = new BasicDBObject(); sb.put(orderBy, 1);// w w w . j a v a 2 s. c om cursor = cursor.sort(sb); } while (cursor.hasNext()) { BasicDBObject dbObject = (BasicDBObject) cursor.next(); dbObject.remove("_id"); ObjectMapper mapper = new ObjectMapper(); Object res = mapper.convertValue(dbObject, destinationClass); result.add(res); } return result; }
From source file:it.sayservice.platform.smartplanner.otp.OTPStorage.java
License:Apache License
public List<Object> getObjectsByFields(MongoTemplate template, Map<String, Object> map, String collectionName, Class destinationClass, String orderBy) { DBCollection collection = template.getCollection(collectionName); List<Object> result = new ArrayList<Object>(); QueryBuilder qb = QueryBuilder.start(); for (String key : map.keySet()) { qb = qb.and(key).is(map.get(key)); }/*from ww w.j av a 2 s.c o m*/ DBCursor cursor = collection.find(qb.get()); if (orderBy != null) { BasicDBObject sb = new BasicDBObject(); sb.put(orderBy, 1); cursor = cursor.sort(sb); } while (cursor.hasNext()) { BasicDBObject dbObject = (BasicDBObject) cursor.next(); dbObject.remove("_id"); ObjectMapper mapper = new ObjectMapper(); mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); Object res = mapper.convertValue(dbObject, destinationClass); result.add(res); } return result; }
From source file:it.sayservice.platform.smartplanner.otp.OTPStorage.java
License:Apache License
public List<Object> getObjectsByQuery(MongoTemplate template, DBObject query, String collectionName, Class destinationClass, String orderBy, DBObject... fields) { DBCollection collection = template.getCollection(collectionName); List<Object> result = new ArrayList<Object>(); DBCursor cursor; if (fields.length == 0) { cursor = collection.find(query); } else {/*from w ww . j a va2 s .c o m*/ cursor = collection.find(query, fields[0]); } if (orderBy != null) { BasicDBObject sb = new BasicDBObject(); sb.put(orderBy, 1); cursor = cursor.sort(sb); } while (cursor.hasNext()) { BasicDBObject dbObject = (BasicDBObject) cursor.next(); dbObject.remove("_id"); ObjectMapper mapper = new ObjectMapper(); mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); Object res = mapper.convertValue(dbObject, destinationClass); result.add(res); } return result; }
From source file:it.sayservice.platform.smartplanner.otp.OTPStorage.java
License:Apache License
public List<Object> getPagedObjectsByQuery(MongoTemplate template, DBObject query, String collectionName, Class destinationClass, String orderBy, int pageSize, int pageN) { DBCollection collection = template.getCollection(collectionName); List<Object> result = new ArrayList<Object>(); DBCursor cursor = collection.find(query).skip(pageN * pageSize).limit(pageSize); if (orderBy != null) { BasicDBObject sb = new BasicDBObject(); sb.put(orderBy, 1);//from w ww .j a va 2 s .c om cursor = cursor.sort(sb); } while (cursor.hasNext()) { BasicDBObject dbObject = (BasicDBObject) cursor.next(); dbObject.remove("_id"); ObjectMapper mapper = new ObjectMapper(); mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); Object res = mapper.convertValue(dbObject, destinationClass); result.add(res); } return result; }
From source file:jahspotify.storage.statistics.MongoDBHistoricalStorage.java
License:Apache License
@Override public Collection<TrackHistory> getHistory(final int index, final int count, final HistoryCriteria... historyCriterias) { final DBCollection tracks = _db.getCollection("history"); final DBCursor dbObjects = tracks.find(); dbObjects.skip(index);/*from ww w . java 2s . c om*/ dbObjects.limit(count); final BasicDBObject orderBy = new BasicDBObject(); orderBy.put("startTime", -1); dbObjects.sort(orderBy); return new AbstractCollection<TrackHistory>() { @Override public Iterator<TrackHistory> iterator() { return new MongoDBHistoryCursor(dbObjects); } @Override public int size() { return dbObjects.size(); } }; }
From source file:jahspotify.storage.statistics.MongoDBHistoricalStorage.java
License:Apache License
@Override public TrackStatistics getTrackStatistics(final Link trackLink) { final DBCollection tracks = _db.getCollection("history"); final BasicDBObject query = new BasicDBObject(); query.put("trackLink.id", trackLink.getId()); final DBCursor dbObjects = tracks.find(query); final BasicDBObject orderBy = new BasicDBObject(); orderBy.put("startTime", -1); dbObjects.sort(orderBy); TrackStatistics trackStatistics = new TrackStatistics(); int numCompleted = 0; int numSkipped = 0; int totalSecondsPlayed = 0; if (dbObjects.hasNext()) { final TrackHistory trackHistory = _gson.fromJson(JSON.serialize(dbObjects.next()), TrackHistory.class); trackStatistics.setTrackLink(trackHistory.getTrackLink()); trackStatistics.setLastPlayed(trackHistory.getStartTime()); totalSecondsPlayed += trackHistory.getSecondsPlayed(); numSkipped += trackHistory.isCompleteTrackPlayed() ? 0 : 1; numCompleted += trackHistory.isCompleteTrackPlayed() ? 1 : 0; if (!dbObjects.hasNext()) { trackStatistics.setFirstPlayed(trackHistory.getStartTime()); }/*from w ww . ja v a 2 s . co m*/ } while (dbObjects.hasNext()) { final TrackHistory trackHistory = _gson.fromJson(JSON.serialize(dbObjects.next()), TrackHistory.class); totalSecondsPlayed += trackHistory.getSecondsPlayed(); numSkipped += trackHistory.isCompleteTrackPlayed() ? 0 : 1; numCompleted += trackHistory.isCompleteTrackPlayed() ? 1 : 0; if (!dbObjects.hasNext()) { trackStatistics.setFirstPlayed(trackHistory.getStartTime()); } } trackStatistics.setNumTimesCompleted(numCompleted); trackStatistics.setNumTimesSkipped(numSkipped); trackStatistics.setNumTimesPlayed(numCompleted + numSkipped); trackStatistics.setTotalPlaytime(totalSecondsPlayed); return trackStatistics; }
From source file:me.carbou.mathieu.tictactoe.db.DBCollection.java
License:Apache License
public Stream<Map<String, Object>> find(Map where, Map fields, Map sort, Function<Map, Map> transform, int limit, int skip) { final DBCursor cursor = getCollection().find(new BasicDBObject(addWhere(where)), new BasicDBObject(preFind(fields))); if (!sort.isEmpty()) cursor.sort(new BasicDBObject(sort)); if (skip > 0) cursor.skip(skip);/*from w w w.ja v a 2 s .c o m*/ if (limit > DB.NO_LIMIT) cursor.limit(limit); int est = cursor.size(); Spliterator<Map<String, Object>> spliterator = new Spliterators.AbstractSpliterator<Map<String, Object>>( est, NONNULL | ORDERED | SIZED | IMMUTABLE) { @Override public boolean tryAdvance(Consumer<? super Map<String, Object>> action) { if (cursor.hasNext()) { action.accept(postFind(where, cursor.next(), transform)); return true; } else { cursor.close(); return false; } } }; return StreamSupport.stream(spliterator, false); }
From source file:me.lightspeed7.mongofs.gridfs.GridFS.java
License:Apache License
/** * Finds a list of files matching the given query. * //from www. j a va 2 s . c om * @param query * the filter to apply * @param sort * the fields to sort with * @return list of gridfs files * @throws com.mongodb.MongoException */ public List<GridFSDBFile> find(final DBObject query, final DBObject sort) { List<GridFSDBFile> files = new ArrayList<GridFSDBFile>(); DBCursor cursor = filesCollection.find(query); if (sort != null) { cursor.sort(sort); } try { while (cursor.hasNext()) { files.add(injectGridFSInstance(cursor.next())); } } finally { cursor.close(); } return Collections.unmodifiableList(files); }
From source file:me.yyam.mongodbutils.MongoDbOperater.java
public List find(QueryInfo queryInfo) { DB db = mongoClient.getDB(queryInfo.dbName); DBCollection coll = db.getCollection(queryInfo.collName); DBCursor cursor = null; if (queryInfo.query != null) { cursor = coll.find(queryInfo.query); } else {/*www . j a v a2 s . c o m*/ cursor = coll.find(); } if (queryInfo.order != null) { cursor = cursor.sort(queryInfo.order); } if (queryInfo.limit != null) { cursor.limit(queryInfo.limit.intValue()); } if (queryInfo.skip != null) { cursor.skip(queryInfo.skip.intValue()); } return cursor2list(cursor); }
From source file:me.yyam.mongodbutils.MongoDbOperater.java
public List<Map> find(DBCollection coll, DBObject query, DBObject order, int start, int limit) { DBCursor cursor = null; if (query == null) { cursor = coll.find();/*from ww w. j a v a 2 s .c o m*/ } else { cursor = coll.find(query); } if (order != null) cursor = cursor.sort(order); if (start != 0 && limit != 0) { cursor.skip(start).limit(limit); } if (start == 0 && limit != 0) { cursor.limit(limit); } return cursor2list(cursor); }