/** * Do aggregation with options. */ //@Test public void testWithOptions(){ connectDB(); BookDao dao = new BookDao(); Iterable<DBObject> it = dao.aggregate() .setOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build()) .sort("{price : -1}") .results(); for(DBObject dbo : it){ System.out.println(dbo.get("price")); } disconnectDB(); }
@Test public void testOut() { checkMinServerVersion(2.6); getDs().save(asList(new Book("The Banquet", "Dante", 2), new Book("Divine Comedy", "Dante", 1), new Book("Eclogues", "Dante", 2), new Book("The Odyssey", "Homer", 10), new Book("Iliad", "Homer", 10))); AggregationOptions options = builder() .build(); Iterator<Author> aggregate = getDs().createAggregation(Book.class) .group("author", grouping("books", push("title"))) .out(Author.class, options); Assert.assertEquals(2, getDs().getCollection(Author.class).count()); Author author = aggregate.next(); Assert.assertEquals("Homer", author.name); Assert.assertEquals(asList("The Odyssey", "Iliad"), author.books); getDs().createAggregation(Book.class) .group("author", grouping("books", push("title"))) .out("different", Author.class); Assert.assertEquals(2, getDb().getCollection("different").count()); }
@Override public Object executeQuery(QueryProvider queryProvider) throws MongoQueryException { Iterator<String> iterator = (Iterator) queryProvider; int i = 0; Aggregate aggregate = null; while (iterator.hasNext()) { String query = iterator.next(); if (i++ == 0) { aggregate = jongo.getCollection(queryProvider.getCollectionName()).aggregate(query); } else { Assert.notNull(aggregate, UNEXPECTED_NULL_AGGREGATE_QUERY); aggregate.and(query); } } Assert.notNull(aggregate, UNEXPECTED_NULL_AGGREGATE_QUERY); aggregate.options(AggregationOptions.builder() .allowDiskUse(queryProvider.isAllowDiskUse()) .maxTime(queryProvider.getMaxTimeMS(), TimeUnit.MILLISECONDS) .build()); ResultsIterator resultsIterator = aggregate.as(HashMap.class); if (resultsIterator == null || !resultsIterator.hasNext() || Void.TYPE .equals(queryProvider.getMethodReturnType())) { return null; } final String resultKey = queryProvider.getQueryResultKey(); if (isPageReturnType(queryProvider) && !queryProvider.isAggregate2()) { throw new IllegalArgumentException("Page can be used as a return type only with @Aggregate2 annotation"); } if (!queryProvider.isPageable() || (queryProvider.isPageable() && List.class.isAssignableFrom(queryProvider.getMethodReturnType()))) { return getNonPageResults(queryProvider, resultsIterator, resultKey); } else if (queryProvider.isPageable() && isPageReturnType(queryProvider)) { return getPageableResults(queryProvider, resultsIterator); } throw new MongoQueryException(QUERY_RETURN_ERROR_STR); }
@GET @Produces("application/json") @Path("/tags/{product}/{major}.{minor}.{servicePack}/{build}") public DBObject getTagList(@BeanParam final Coordinates coordinates) { final DB bdd = this.client.getDB("bdd"); final DBCollection features = bdd.getCollection("features"); List<BasicDBObject> objectList = new ArrayList<BasicDBObject>(); // Build objects for aggregation pipeline // id option: returns each tag with a list of associated feature ids objectList.add(new BasicDBObject("$match", coordinates.getReportCoordinatesQueryObject())); final DBObject fields = new BasicDBObject("tags.name", 1); fields.put("_id", 0); // comment out for id option objectList.add(new BasicDBObject("$project", fields)); objectList.add(new BasicDBObject("$unwind", "$tags")); final DBObject groupFields = new BasicDBObject("_id", "$tags.name"); // groupFields.put("features", new BasicDBObject("$addToSet", "$_id")); //comment in for id option groupFields.put("amount", new BasicDBObject("$sum", 1)); objectList.add(new BasicDBObject("$group", groupFields)); objectList.add(new BasicDBObject("$sort", new BasicDBObject("amount", -1))); AggregationOptions options = AggregationOptions.builder().build(); final Cursor output = features.aggregate(objectList, options); // get _ids from each entry of output.result final BasicDBList returns = new BasicDBList(); while(output.hasNext()) { returns.add(output.next().get("_id").toString()); } return returns; }
@Test public void testDirect() throws Exception { Command cmd = this.utility.parseCommand("SELECT * FROM Customers"); MongoDBConnection connection = Mockito.mock(MongoDBConnection.class); ExecutionContext context = Mockito.mock(ExecutionContext.class); DBCollection dbCollection = Mockito.mock(DBCollection.class); DB db = Mockito.mock(DB.class); Mockito.stub(db.getCollection("MyTable")).toReturn(dbCollection); Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true); Mockito.stub(connection.getDatabase()).toReturn(db); AggregationOutput output = Mockito.mock(AggregationOutput.class); Mockito.stub(output.results()).toReturn(new ArrayList<DBObject>()); Mockito.stub(dbCollection.aggregate(Mockito.any(DBObject.class),Mockito.any(DBObject.class))).toReturn(output); Argument arg = new Argument(Direction.IN, null, String.class, null); arg.setArgumentValue(new Literal("MyTable;{$match:{\"id\":\"$1\"}};{$project:{\"_m0\":\"$user\"}}", String.class)); Argument arg2 = new Argument(Direction.IN, null, String.class, null); arg2.setArgumentValue(new Literal("foo", String.class)); ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg, arg2), cmd, context, this.utility.createRuntimeMetadata(), connection); execution.execute(); List<DBObject> pipeline = TestMongoDBQueryExecution.buildArray(new BasicDBObject("$match", new BasicDBObject("id", "foo")), new BasicDBObject("$project", new BasicDBObject("_m0", "$user"))); Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class)); }
@Override public <U> Iterator<U> aggregate(final String collectionName, final Class<U> target, final AggregationOptions options, final ReadPreference readPreference) { LOG.debug("stages = " + stages); Cursor cursor = collection.aggregate(stages, options, readPreference); return new MorphiaIterator<U, U>(datastore, cursor, mapper, target, collectionName, mapper.createEntityCache()); }
public BuguAggregation setOptions(AggregationOptions options){ this.options = options; return this; }
@Override public <U> Iterator<U> aggregate(final Class<U> target) { return aggregate(target, AggregationOptions.builder().build(), collection.getReadPreference()); }
@Override public <U> Iterator<U> aggregate(final Class<U> target, final AggregationOptions options) { return aggregate(target, options, collection.getReadPreference()); }
@Override public <U> Iterator<U> aggregate(final Class<U> target, final AggregationOptions options, final ReadPreference readPreference) { return aggregate(datastore.getCollection(target).getName(), target, options, readPreference); }
@Override public <U> Iterator<U> out(final Class<U> target, final AggregationOptions options) { return out(datastore.getCollection(target).getName(), target, options); }
@Override public <U> Iterator<U> out(final String collectionName, final Class<U> target) { return out(collectionName, target, AggregationOptions.builder().build()); }
@Override public <U> Iterator<U> out(final String collectionName, final Class<U> target, final AggregationOptions options) { stages.add(new BasicDBObject("$out", collectionName)); return aggregate(target, options); }
/** * Executes the pipeline and aggregates the output in to the type mapped by the target type. * * @param target The class to use when iterating over the results * @param options The options to apply to this aggregation * @param <U> type of the results * @return an iterator of the computed results */ <U> Iterator<U> aggregate(Class<U> target, AggregationOptions options);
/** * Executes the pipeline and aggregates the output in to the type mapped by the target type. * * @param target The class to use when iterating over the results * @param options The options to apply to this aggregation * @param readPreference The read preference to apply to this pipeline * @param <U> type of the results * @return an iterator of the computed results */ <U> Iterator<U> aggregate(Class<U> target, AggregationOptions options, ReadPreference readPreference);
/** * Executes the pipeline and aggregates the output in to the type mapped by the target type. * * @param collectionName The collection in which to store the results of the aggregation overriding the mapped value in target * @param target The class to use when iterating over the results * @param options The options to apply to this aggregation * @param readPreference The read preference to apply to this pipeline * @param <U> type of the results * @return an iterator of the computed results */ <U> Iterator<U> aggregate(String collectionName, Class<U> target, AggregationOptions options, ReadPreference readPreference);
/** * Places the output of the aggregation in the collection mapped by the target type. * * @param target The class to use when iterating over the results * @param options The options to apply to this aggregation * @param <U> type of the results * @return an iterator of the computed results * @mongodb.driver.manual reference/operator/aggregation/out $out */ <U> Iterator<U> out(Class<U> target, AggregationOptions options);
/** * Places the output of the aggregation in the collection mapped by the target type. * * @param collectionName The collection in which to store the results of the aggregation overriding the mapped value in target * @param target The class to use when iterating over the results * @param options The options to apply to this aggregation * @param <U> type of the results * @return an iterator of the computed results * @mongodb.driver.manual reference/operator/aggregation/out $out */ <U> Iterator<U> out(String collectionName, Class<U> target, AggregationOptions options);