/** * Takes `uploadID` and returns all bed names as a json format string * @param uploadID - the year that the data was uploaded * @return String representation of json with all bed names */ public String getGardenLocationsAsJson(String uploadID){ AggregateIterable<Document> documents = plantCollection.aggregate( Arrays.asList( Aggregates.match(eq("uploadID", uploadID)), //!! Order is important here Aggregates.group("$gardenLocation"), Aggregates.sort(Sorts.ascending("_id")) )); List<Document> listDoc = new ArrayList<>(); for (Document doc : documents) { listDoc.add(doc); } listDoc.sort(new BedComparator()); return JSON.serialize(listDoc); }
/** * List all plants within the database, filtered by uploadId, gardenLocation and commonName * @param queryParams * @param uploadId * @return */ public String listPlants(Map<String, String[]> queryParams, String uploadId) { if (!ExcelParser.isValidUploadId(db, uploadId)) return "null"; //Create a filter based on query params Document filterDoc = new Document(); filterDoc.append("uploadId", uploadId); if (queryParams.containsKey("gardenLocation")) { String location =(queryParams.get("gardenLocation")[0]); filterDoc = filterDoc.append("gardenLocation", location); } if (queryParams.containsKey("commonName")) { String commonName =(queryParams.get("commonName")[0]); filterDoc = filterDoc.append("commonName", commonName); } FindIterable<Document> matchingPlants = plantCollection.find(filterDoc); matchingPlants.sort(Sorts.ascending("commonName", "cultivar")); return JSON.serialize(matchingPlants); }
/** * * @return a date-sorted List of all the distinct uploadIds in the DB */ public static List<String> listUploadIds(MongoDatabase database) { MongoCollection<Document> plantCollection = database.getCollection("plants"); AggregateIterable<Document> documents = plantCollection.aggregate( Arrays.asList( Aggregates.group("$uploadId"), Aggregates.sort(Sorts.ascending("_id")) )); List<String> lst = new LinkedList<>(); for(Document d: documents) { lst.add(d.getString("_id")); } return lst; }
@Deprecated public List<ResourceStatus> getStatuses(String environmentName, String resourceId, Date from, Date to) { // consider month switch during data extraction if will be used switchCollection(from); return thisCollection.find(and( eq("environmentName", environmentName), gte("updated", from), lte("updated", to), eq("resource.resourceId", resourceId)) ) .sort(Sorts.ascending("updated")) .map(DocumentMapper::resourceStatusFromDocument) .into(new ArrayList<>()); }
@Test public void ageCounts() { AggregateIterable<Document> documents = userDocuments.aggregate( Arrays.asList( /* * Groups data by the "age" field, and then counts * the number of documents with each given age. * This creates a new "constructed document" that * has "age" as it's "_id", and the count as the * "ageCount" field. */ Aggregates.group("$age", Accumulators.sum("ageCount", 1)), Aggregates.sort(Sorts.ascending("_id")) ) ); List<Document> docs = intoList(documents); assertEquals("Should be two distinct ages", 2, docs.size()); assertEquals(docs.get(0).get("_id"), 25); assertEquals(docs.get(0).get("ageCount"), 1); assertEquals(docs.get(1).get("_id"), 37); assertEquals(docs.get(1).get("ageCount"), 2); }
@Test public void averageAge() { AggregateIterable<Document> documents = userDocuments.aggregate( Arrays.asList( Aggregates.group("$company", Accumulators.avg("averageAge", "$age")), Aggregates.sort(Sorts.ascending("_id")) )); List<Document> docs = intoList(documents); assertEquals("Should be three companies", 3, docs.size()); assertEquals("Frogs, Inc.", docs.get(0).get("_id")); assertEquals(37.0, docs.get(0).get("averageAge")); assertEquals("IBM", docs.get(1).get("_id")); assertEquals(37.0, docs.get(1).get("averageAge")); assertEquals("UMM", docs.get(2).get("_id")); assertEquals(25.0, docs.get(2).get("averageAge")); }
private static List<Bson> getServerQuery(Bson filter) { final List<Bson> pipeline = new ArrayList<>(6); if (filter != ALL) { pipeline.add(Aggregates.match(filter)); } pipeline.add(Aggregates.unwind("$deployments", new UnwindOptions().preserveNullAndEmptyArrays(true))); pipeline.add(Aggregates.lookup(Collections.APPLICATIONS, "deployments.applicationId", "id", "applications")); pipeline.add(Aggregates.unwind("$applications", new UnwindOptions().preserveNullAndEmptyArrays(true))); pipeline.add(Aggregates.group( new Document().append("hostname", "$hostname").append("environment", "$environment"), new BsonField("fqdn", new Document("$first", "$fqdn")), new BsonField("description", new Document("$first", "$description")), new BsonField("os", new Document("$first", "$os")), new BsonField("network", new Document("$first", "$network")), new BsonField("meta", new Document("$first", "$meta")), new BsonField("attributes", new Document("$first", "$attributes")), new BsonField("applications", new Document("$push", "$applications")), new BsonField("deployments", new Document("$push", "$deployments")))); pipeline.add(Aggregates.sort(Sorts.ascending("_id"))); return pipeline; }
@Override public List<Suite> listSuites(DBKey dbKey) throws StorageException { MongoCollection<Document> metadata = getMetadataCollection(dbKey); LOGGER.debug("Fetching all suites for company: `{}`, project: `{}`.", dbKey.getCompany(), dbKey.getProject()); final FindIterable<Document> found = metadata .find() .sort(Sorts.descending(SUITE_VERSION_PARAM_NAME)); return FluentIterable.from(found).transform(new Function<Document, Suite>() { @Override public Suite apply(Document result) { return new DocumentConverter(result).toSuite(); } }).toList(); }
@Override public Timeserie serie(String name) { Set<Data> results = StreamSupport .stream(timeseries .find(Filters.eq("type", name)) .sort(Sorts.ascending("timestamp_hour")) .limit(3) .map(doc -> { LocalDateTime hour = LocalDateTime.ofEpochSecond(((Date) doc.get("timestamp_hour")).getTime() / 1000, 0, ZoneOffset.UTC); Map<String, Long> values = (Map<String, Long>) doc.get("values"); List<Data> mapped = values.entrySet().stream().map(elt -> Data.of(hour.plusMinutes(Long.valueOf(elt.getKey())), elt.getValue())) .collect(Collectors.toList()); return mapped; }).spliterator(), true).flatMap(elts -> elts.stream()).collect(TreeSet::new, Set::add, (left, right) -> { left.addAll(right); }); Timeserie timeserie = new Timeserie(); timeserie.addAll(results); return timeserie; }
/** * Get a json containing a list of commonNames sorted by common name * @param uploadID * @return */ public String getCommonNamesJSON(String uploadID){ if (!ExcelParser.isValidUploadId(db, uploadID)) return "null"; AggregateIterable<Document> documents = plantCollection.aggregate( Arrays.asList( Aggregates.match(eq("uploadId", uploadID)), //!! Order is important here Aggregates.group("$commonName"), Aggregates.sort(Sorts.ascending("commonName")) )); return JSON.serialize(documents); }
@Override public Bson sort() { if ((isBlank(caller) || isRegex(caller)) && (isBlank(callee) || isRegex(callee))) { return null; } return Sorts.ascending("millis"); }
public List<Message> get(OfflineMessagesMeta meta) { Document query = new Document("topic", meta.topic).append("msgid", new Document().append("$gt", meta.start).append("$lte", meta.end)); FindIterable<Document> list = this.mongo.find(query).sort(Sorts.ascending("msgid")); List<Message> messages = new ArrayList<>(); for (Document doc : list) { messages.add(new Message(doc.getString("topic"), doc.getLong("msgid"), ((Binary) doc.get("msg")).getData())); } return messages; }
public String getGardenLocationsAsJson(String uploadID) { AggregateIterable<Document> documents = plantCollection.aggregate( Arrays.asList( Aggregates.match(eq("uploadId", uploadID)), //!! Order is important here Aggregates.group("$gardenLocation"), Aggregates.sort(Sorts.ascending("_id")) )); return JSON.serialize(documents); }
/** * * @return a sorted JSON array of all the distinct uploadIds in the DB */ public String listUploadIds() { AggregateIterable<Document> documents = plantCollection.aggregate( Arrays.asList( Aggregates.group("$uploadId"), Aggregates.sort(Sorts.ascending("_id")) )); List<String> lst = new LinkedList<>(); for(Document d: documents) { lst.add(d.getString("_id")); } return JSON.serialize(lst); // return JSON.serialize(plantCollection.distinct("uploadId","".getClass())); }
@Test public void over25SortedByName() { FindIterable<Document> documents = userDocuments.find(gt("age", 25)) .sort(Sorts.ascending("name")); List<Document> docs = intoList(documents); assertEquals("Should be 2", 2, docs.size()); assertEquals("First should be Jamie", "Jamie", docs.get(0).get("name")); assertEquals("Second should be Pat", "Pat", docs.get(1).get("name")); }
@Test public void justNameAndEmailNoIdSortedByCompany() { FindIterable<Document> documents = userDocuments.find() .sort(Sorts.ascending("company")) .projection(fields(include("name", "email"), excludeId())); List<Document> docs = intoList(documents); assertEquals("Should be 3", 3, docs.size()); assertEquals("First should be Jamie", "Jamie", docs.get(0).get("name")); assertNotNull("First should have email", docs.get(0).get("email")); assertNull("First shouldn't have 'company'", docs.get(0).get("company")); assertNull("First should not have '_id'", docs.get(0).get("_id")); }
/** * * @return a sorted JSON array of all the distinct uploadIDs in plant collection of the DB */ public List<String> listUploadIDs() { AggregateIterable<Document> documents = plantCollection.aggregate( Arrays.asList( Aggregates.group("$uploadID"), Aggregates.sort(Sorts.ascending("_id")) )); List<String> lst = new LinkedList<>(); for(Document d: documents) { lst.add(d.getString("_id")); } return lst; // return JSON.serialize(plantCollection.distinct("uploadID","".getClass())); }
private int getLargestID() throws NotConnectedException { try { MongoCollection<Document> collection = getCollection(); if (collection.count() <= 0) { return 0; } int largestID = (int) collection.find().sort(Sorts.descending("internalDbID")).limit(1).first().get("internalDbID"); return largestID; } catch (Exception e) { BackendprocessService.getInstance().logger.error("couldnt find largest id " + e.toString()); } return 1; }
public String getAverageAgeByCompany() { AggregateIterable<Document> documents = userCollection.aggregate( Arrays.asList( Aggregates.group("$company", Accumulators.avg("averageAge", "$age")), Aggregates.sort(Sorts.ascending("_id")) )); System.err.println(JSON.serialize(documents)); return JSON.serialize(documents); }
private Bson getSorts(List<QuerySortOrder> list) { if (list == null || list.size() == 0) return defaultSort; List<Bson> newSorts = new ArrayList<>(); for (QuerySortOrder querySortOrder : list) { if (querySortOrder.getDirection() == null || querySortOrder.getDirection().equals(SortDirection.ASCENDING)) newSorts.add(Sorts.ascending(querySortOrder.getSorted())); else newSorts.add(Sorts.descending(querySortOrder.getSorted())); } return Sorts.orderBy(newSorts); }
@Test public void testFind() { FindIterable<Document> find = coll.find(Filters.eq("name", "Alto"), Document.class) .sort(Sorts.ascending("color")); List<Document> docList = toDocumentList(find); assertEquals(4, docList.size()); find = coll.find(Filters.eq("name", "Alto")).sort(Sorts.ascending("color")); docList = toDocumentList(find); assertEquals(4, docList.size()); find = coll.find(Document.class).filter(Filters.eq("name", "Alto")).sort(Sorts.ascending("color")); docList = toDocumentList(find); assertEquals(4, docList.size()); find = coll.find().filter(Filters.eq("name", "Alto")).sort(Sorts.ascending("color")); docList = toDocumentList(find); assertEquals(4, docList.size()); find = coll.find().filter(Filters.eq("name", "Alto")).sort(Sorts.ascending("color")).batchSize(123) .collation(Collation.builder().build()).cursorType(CursorType.NonTailable).limit(2) .maxAwaitTime(12, TimeUnit.DAYS).maxTime(12, TimeUnit.DAYS).noCursorTimeout(true).oplogReplay(false) .partial(false).skip(1); docList = toDocumentList(find); assertEquals(2, docList.size()); Document firstFind = coll.find().filter(Filters.eq("name", "Alto")).sort(Sorts.ascending("color")).first(); Assert.assertNotNull(firstFind); coll.find().filter(Filters.eq("name", "Alto")).forEach(new Block<Document>() { @Override public void apply(Document t) { System.out.println(t.get("name")); } }); }
private <T> PaginatedCollection<T> textSearch(String query, String collection, Function<Document,T> mapper) { return RestHelper.paginatedList( database.getCollection(collection) .find(Filters.text(query)) .projection(Projections.metaTextScore("score")) .sort(Sorts.metaTextScore("score")) .map(mapper) ); }
@Override public Suite getSuite(DBKey dbKey, String correlationId) throws StorageException { MongoCollection<Document> metadata = getMetadataCollection(dbKey); LOGGER.debug("Fetching suite with correlationId: {} ", correlationId); final FindIterable<Document> found = metadata .find(Filters.eq(CORRELATION_ID_PARAM_NAME, correlationId)) .sort(Sorts.descending(SUITE_VERSION_PARAM_NAME)) .limit(1); final Document result = found.first(); return new DocumentConverter(result).toSuite(); }
@Override public Suite getLatestRun(DBKey dbKey, String name) throws StorageException { MongoCollection<Document> metadata = getMetadataCollection(dbKey); LOGGER.debug("Fetching latest suite run for company: `{}`, project: `{}`, name `{}`.", dbKey.getCompany(), dbKey.getProject(), name); final FindIterable<Document> found = metadata .find(Filters.eq("name", name)) .sort(Sorts.descending(SUITE_VERSION_PARAM_NAME)) .limit(1); final Document result = found.first(); return new DocumentConverter(result).toSuite(); }
public Bson toSort(List<OrderSpecifier<?>> orderBys) { List<Bson> bsons = orderBys.stream() .map(orderBy -> { Object key = orderBy.getTarget().accept(this, null); return orderBy.getOrder() == Order.ASC ? Sorts.ascending((String) key) : Sorts.descending((String) key); }) .collect(Collectors.toList()); return Sorts.orderBy(bsons); }
@ExtDirectMethod(STORE_READ) public ExtDirectStoreResult<User> read(ExtDirectStoreReadRequest request) { List<Bson> andFilters = new ArrayList<>(); StringFilter filter = request.getFirstFilterForField("filter"); if (filter != null) { List<Bson> orFilters = new ArrayList<>(); orFilters.add(Filters.regex(CUser.loginName, filter.getValue(), "i")); orFilters.add(Filters.regex(CUser.lastName, filter.getValue(), "i")); orFilters.add(Filters.regex(CUser.firstName, filter.getValue(), "i")); orFilters.add(Filters.regex(CUser.email, filter.getValue(), "i")); andFilters.add(Filters.or(orFilters)); } andFilters.add(Filters.eq(CUser.deleted, false)); long total = this.mongoDb.getCollection(User.class) .count(Filters.and(andFilters)); FindIterable<User> find = this.mongoDb.getCollection(User.class) .find(Filters.and(andFilters)); find.sort(Sorts.orderBy(QueryUtil.getSorts(request))); find.skip(request.getStart()); find.limit(request.getLimit()); return new ExtDirectStoreResult<>(total, QueryUtil.toList(find)); }
public static List<Bson> getSorts(ExtDirectStoreReadRequest request) { List<Bson> sorts = new ArrayList<>(); for (SortInfo sortInfo : request.getSorters()) { if (sortInfo.getDirection() == SortDirection.ASCENDING) { sorts.add(Sorts.ascending(sortInfo.getProperty())); } else { sorts.add(Sorts.descending(sortInfo.getProperty())); } } return sorts; }
@Override public List<Event> page(int from, int size) { return Lists.newArrayList(timeline.find().sort(Sorts.descending("time")).skip(from).limit(size).map(doc -> { try { return mapper.readValue(doc.toJson(), Event.class); } catch (Exception e) { throw Throwables.propagate(e); } })); }
private QuerySort addSortInformation( boolean asc ) { curQueriedProperty.forEach( parameterProperty -> curSorts.add( parameterProperty.getMongoName() ) ); if ( asc ) { sorts.add( Sorts.ascending( curSorts ) ); } else { sorts.add( Sorts.descending( curSorts ) ); } curSorts.clear(); curQueriedProperty.clear(); return querySort.get(); }
@Override public Bson sort() { return Sorts.ascending("millis", "nanos"); }
public List<SyncMap> getAllMapping() { List<SyncMap> mapList = new ArrayList<SyncMap>(); syncMappings.find().projection(Projections.exclude(SyncAttrs.MAP_OBJECT)).sort(Sorts.descending(SyncAttrs.CREATED_ON)).into(mapList); return mapList; }
public List<SyncMap> getMappingByMapType(String mapType) { List<SyncMap> mapList = new ArrayList<SyncMap>(); syncMappings.find(Filters.eq(SyncAttrs.MAP_TYPE, mapType)).projection(Projections.exclude(SyncAttrs.MAP_OBJECT)).sort(Sorts.descending(SyncAttrs.CREATED_ON)).into(mapList); return mapList; }