@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> aggregate(final Class<U> target) { return aggregate(target, AggregationOptions.builder().build(), collection.getReadPreference()); }
/** * Method implements aggregation framework. * * @param pipeline operations to be performed in the aggregation pipeline * @param readPreference the read preference specifying where to run the query * @return the aggregation's result set * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation * @mongodb.server.release 2.2 * @deprecated Use {@link #aggregate(List, AggregationOptions, ReadPreference)} )} instead */ @Deprecated @SuppressWarnings("unchecked") public AggregationOutput aggregate(final List<? extends DBObject> pipeline, final ReadPreference readPreference) { Cursor cursor = aggregate(pipeline, AggregationOptions.builder().build(), readPreference, false); if (cursor == null) { return new AggregationOutput(Collections.<DBObject>emptyList()); } else { List<DBObject> results = new ArrayList<DBObject>(); while (cursor.hasNext()) { results.add(cursor.next()); } return new AggregationOutput(results); } }
@Test public void testDateToString() throws ParseException { checkMinServerVersion(3.0); Date joined = new SimpleDateFormat("yyyy-MM-dd z").parse("2016-05-01 UTC"); getDs().save(new User("John Doe", joined)); AggregationPipeline pipeline = getDs() .createAggregation(User.class) .project(projection("string", expression("$dateToString", new BasicDBObject("format", "%Y-%m-%d") .append("date", "$joined")))); Iterator<StringDates> aggregate = pipeline.aggregate(StringDates.class, builder() .build()); while (aggregate.hasNext()) { StringDates next = aggregate.next(); Assert.assertEquals("2016-05-01", next.string); } }
@Test public void testCollation() { checkMinServerVersion(3.4); getDs().save(asList(new User("john doe", new Date()), new User("John Doe", new Date()))); Query query = getDs().find(User.class).field("name").equal("john doe"); AggregationPipeline pipeline = getDs() .createAggregation(User.class) .match(query); Assert.assertEquals(1, count(pipeline.aggregate(User.class))); Assert.assertEquals(2, count(pipeline.aggregate(User.class, builder() .collation(Collation.builder() .locale("en") .collationStrength( CollationStrength.SECONDARY) .build()).build()))); }
@Test public void testBypassDocumentValidation() { checkMinServerVersion(3.2); getDs().save(asList(new User("john doe", new Date()), new User("John Doe", new Date()))); MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME); database.getCollection("out_users").drop(); database.createCollection("out_users", new CreateCollectionOptions() .validationOptions(new ValidationOptions() .validator(Document.parse("{ \"age\" : { \"gte\" : 13 } }")))); try { getDs() .createAggregation(User.class) .match(getDs().find(User.class).field("name").equal("john doe")) .out("out_users", User.class); fail("Document validation should have complained."); } catch (MongoCommandException e) { // expected } getDs() .createAggregation(User.class) .match(getDs().find(User.class).field("name").equal("john doe")) .out("out_users", User.class, builder() .bypassDocumentValidation(true) .build()); Assert.assertEquals(1, getAds().find("out_users", User.class).count()); }
@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 <U> Iterator<U> out(final String collectionName, final Class<U> target) { return out(collectionName, target, AggregationOptions.builder().build()); }
@Override public Iterable<DBObject> results(){ if(options == null){ options = AggregationOptions.builder().build(); } final Iterator<DBObject> it = coll.aggregate(pipeline, options); return new Iterable<DBObject>() { @Override public Iterator<DBObject> iterator() { return it; } }; }
@Override public <U> Iterator<U> aggregate(final Class<U> target) { return aggregate(target, AggregationOptions.builder().build(), collection.getReadPreference()); }
/** * Method implements aggregation framework. * * @param pipeline operations to be performed in the aggregation pipeline * @param readPreference the read preference specifying where to run the query * @return the aggregation's result set * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation * @mongodb.server.release 2.2 * @deprecated Use {@link #aggregate(List, AggregationOptions, ReadPreference)} )} instead */ @Deprecated @SuppressWarnings("unchecked") public AggregationOutput aggregate(final List<? extends DBObject> pipeline, final ReadPreference readPreference) { Cursor cursor = aggregate(pipeline, AggregationOptions.builder().build(), readPreference, false); if (cursor == null) { return new AggregationOutput(Collections.<DBObject>emptyList()); } else { List<DBObject> results = new ArrayList<DBObject>(); while (cursor.hasNext()) { results.add(cursor.next()); } return new AggregationOutput(results); } }
public AggregationOptions getOptions(int batchSize) { if (this.version.compareTo(TWO_4) < 0) { return AggregationOptions.builder().batchSize(batchSize).outputMode(AggregationOptions.OutputMode.INLINE).build(); } return AggregationOptions.builder().batchSize(batchSize).outputMode(AggregationOptions.OutputMode.CURSOR) .allowDiskUse(useDisk()).build(); } }
/** * 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(); }