List of usage examples for com.mongodb.client FindIterable sort
FindIterable<TResult> sort(@Nullable Bson sort);
From source file:anuncius.singleton.MongoHandler.java
public List<Document> getList(String collectionName, int limit, int sort, Document result, String sortParam) { MongoCollection<Document> collection = mainDatabase.getCollection(collectionName); if (collection != null) { FindIterable<Document> iterable; if (result == null) { result = new Document(); }//from w ww . ja v a2s .co m iterable = collection.find(result); if (limit > 0) { iterable = iterable.limit(limit); } if (sortParam != null) { iterable = iterable.sort(new BasicDBObject(sortParam, sort)); } return iterable.into(new ArrayList<>()); } return new ArrayList<>(); }
From source file:com.bluedragon.mongo.MongoCollectionFind.java
License:Open Source License
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException { MongoDatabase db = getMongoDatabase(_session, argStruct); String collection = getNamedStringParam(argStruct, "collection", null); if (collection == null) throwException(_session, "please specify a collection"); cfData query = getNamedParam(argStruct, "query", null); if (query == null) throwException(_session, "please specify query"); int size = getNamedIntParam(argStruct, "size", -1); int skip = getNamedIntParam(argStruct, "skip", -1); cfData sort = getNamedParam(argStruct, "sort", null); cfData fields = getNamedParam(argStruct, "fields", null); try {/*from w w w .j a v a2 s. c o m*/ MongoCollection<Document> col = db.getCollection(collection); // Get the initial cursor FindIterable<Document> cursor; long start = System.currentTimeMillis(); Document qry = getDocument(query); cursor = col.find(qry); if (fields != null) cursor = cursor.projection(getDocument(fields)); // Are we sorting? if (sort != null) cursor = cursor.sort(getDocument(sort)); // Are we limiting if (skip != -1) cursor = cursor.skip(skip); // How many we bringing back if (size != -1) cursor = cursor.limit(size); // Now we can run the query cfArrayData results = cfArrayData.createArray(1); cursor.forEach(new Block<Document>() { @SuppressWarnings("rawtypes") @Override public void apply(final Document st) { try { results.addElement(tagUtils.convertToCfData((Map) st)); } catch (cfmRunTimeException e) { } } }); _session.getDebugRecorder().execMongo(col, "find", qry, System.currentTimeMillis() - start); return results; } catch (MongoException me) { throwException(_session, me.getMessage()); return null; } }
From source file:com.eclipsesource.connect.persistence.MongoStorage.java
License:Open Source License
private FindIterable<Document> prepareIterable(Query<?> query, FindIterable<Document> iterable) { if (query.getLimit() != -1) { iterable.limit(query.getLimit()); }/*from www . ja v a2s . c om*/ if (query.getSortField() != null) { iterable.sort( new Document(query.getSortField(), query.getSortDirection() == SortDirection.ASC ? 1 : -1)); } if (query.getSkip() != Query.UNDEFINED) { iterable.skip(query.getSkip()); } return iterable; }
From source file:com.github.cherimojava.data.mongo.query.QueryInvocationHandler.java
License:Apache License
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); switch (methodName) { case "e": // this is just a handout for the entity to get knowledge of what to check return this.entityProxy.get(); case "where":/* fallthrough */ case "and": return this.specifier.get(); case "iterator": FindIterable it = coll.find(Filters.and(filters.toArray(new Bson[] {}))); if (limit != null) { it.limit(limit);//w w w .jav a 2s .c o m } if (skip != null) { it.skip(skip); } if (sorts.size() > 0) { it.sort(Sorts.orderBy(sorts)); } return it.iterator(); case "count": return coll.count(Filters.and(filters.toArray(new Bson[] {}))); case "limit": limit = (Integer) args[0]; return queryEnd.get(); case "skip": skip = (Integer) args[0]; return queryEnd.get(); case "sort": checkState(!sortSet, "Sorting can be specified only once"); sortSet = true; return querySort.get(); case "desc":/* fallthrough */ case "asc": return addSortInformation("asc".equals(methodName)); case "by": return addSortInformation(args[0] == QuerySort.Sort.ASC); } throw new IllegalStateException("Unknown method found: " + methodName); }
From source file:com.hurence.logisland.service.mongodb.MongoDBControllerService.java
License:Apache License
@Override public List<Document> findMany(Document query, Document sort, int limit) { FindIterable<Document> fi = this.col.find(query); if (limit > 0) { fi = fi.limit(limit);/* ww w . ja v a 2 s. c o m*/ } if (sort != null) { fi = fi.sort(sort); } MongoCursor<Document> cursor = fi.iterator(); List<Document> retVal = new ArrayList<>(); while (cursor.hasNext()) { retVal.add(cursor.next()); } cursor.close(); return retVal; }
From source file:com.kurniakue.data.Record.java
public static <T extends Record> List<T> loadList(T dummy, Document filter, Document sort) { List<T> list = new ArrayList<>(); Class<T> clazz = (Class<T>) dummy.getClass(); MongoCollection collection = dummy.getCollection(); FindIterable<Document> result; if (filter == null) { result = collection.find();/*from w w w .j a v a2 s.c o m*/ } else { result = collection.find(filter); } if (sort != null) { result = result.sort(sort); } for (MongoCursor<Document> iterator = result.iterator(); iterator.hasNext();) { Document document = iterator.next(); T record = Tool.neo(clazz); record.putAll(document); list.add(record); } return list; }
From source file:com.px100systems.data.plugin.storage.mongo.MongoDatabaseStorage.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public <T> List<T> search(String unitName, Class<T> cls, Criteria criteria, List<String> orderBy, Integer limit) {/*from w w w . ja v a 2s .c om*/ SerializationDefinition def = SerializationDefinition.get(cls); if (def == null) throw new RuntimeException("Cannot find SerializationDefinition for " + cls.getSimpleName()); MongoDatabase db = mongoClient.getDatabase(databaseName); FindIterable<Document> query = criteria == null ? db.getCollection(unitName).find() : db.getCollection(unitName).find(criteria.convert(new FilterQueryBuilder())); if (orderBy != null && !orderBy.isEmpty()) if (orderBy.size() == 1) query = query.sort(orderBy(orderBy.get(0))); else { List<Bson> ob = new ArrayList<>(); for (String s : orderBy) ob.add(orderBy(s)); query = query.sort(Sorts.orderBy(ob)); } List<T> result = new ArrayList<>(); MongoCursor<Document> cursor = query.limit(limit).iterator(); try { while (cursor.hasNext()) { T item = (T) def.newInstance(); def.read(cursor.next(), item); result.add(item); } } finally { cursor.close(); } return result; }
From source file:com.px100systems.data.plugin.storage.mongo.MongoDatabaseStorage.java
License:Open Source License
@Override public <T> EntityCursor<T> search(String unitName, Class<T> cls, Criteria criteria, List<String> orderBy) { SerializationDefinition def = SerializationDefinition.get(cls); if (def == null) throw new RuntimeException("Cannot find SerializationDefinition for " + cls.getSimpleName()); MongoDatabase db = mongoClient.getDatabase(databaseName); FindIterable<Document> query = criteria == null ? db.getCollection(unitName).find() : db.getCollection(unitName).find(criteria.convert(new FilterQueryBuilder())); if (orderBy != null && !orderBy.isEmpty()) if (orderBy.size() == 1) query = query.sort(orderBy(orderBy.get(0))); else {/*ww w . ja va 2 s.co m*/ List<Bson> ob = new ArrayList<>(); for (String s : orderBy) ob.add(orderBy(s)); query = query.sort(Sorts.orderBy(ob)); } return new ResultIterator<T>(query.iterator(), def); }
From source file:com.redhat.thermostat.gateway.common.mongodb.executor.MongoExecutor.java
License:Open Source License
public MongoDataResultContainer execGetRequest(MongoCollection<Document> collection, Integer limit, Integer offset, String sort, List<String> queries, String includes, String excludes, Set<String> realms) { FindIterable<Document> documents = collection.find(); MongoDataResultContainer queryDataContainer = new MongoDataResultContainer(); Bson query = MongoRequestFilters.buildQuery(queries, realms); documents = documents.filter(query); long count = collection.count(query); queryDataContainer.setGetReqCount(count); queryDataContainer.setRemainingNumQueryDocuments((int) (count - (limit + offset))); documents = buildProjection(documents, includes, excludes); final Bson sortObject = MongoSortFilters.createSortObject(sort); documents = documents.sort(sortObject).limit(limit).skip(offset).batchSize(limit) .cursorType(CursorType.NonTailable); queryDataContainer.setQueryDataResult(documents); return queryDataContainer; }
From source file:com.redhat.thermostat.gateway.common.mongodb.MongoStorageHandler.java
License:Open Source License
public String getMany(MongoCollection<Document> collection, Bson query, Integer limit, Integer offset, String sort, String includes, String excludes) { FindIterable<Document> documents = query == null ? collection.find() : collection.find(query); documents = buildProjection(documents, includes, excludes); final Bson sortObject = MongoSortFilters.createSortObject(sort); documents = documents.sort(sortObject).limit(limit).skip(offset).batchSize(limit) .cursorType(CursorType.NonTailable); return mongoResponseBuilder.addQueryDocuments(documents).build(); }