use of com.mongodb.Function in project mongo-java-driver by mongodb.
the class MapReduceAcceptanceTest method shouldMapIterableOfDocumentsIntoAnObjectOfYourChoice.
@Test
public void shouldMapIterableOfDocumentsIntoAnObjectOfYourChoice() {
//given
insertLabelData();
//when
MongoIterable<Document> results = collection.mapReduce(" function(){ " + " for ( var i=0; i < this.labels.length; i++ ){ " + " emit( this.labels[i] , 1 ); " + " }" + "}", " function(key,values){ " + " var sum=0; " + " for( var i=0; i < values.length; i++ ) " + " sum += values[i]; " + " return sum;" + "}").filter(//find all IDs greater than 1
new Document("_id", new Document("$gt", 1))).sort(// sort by ID
new Document("_id", 1)).limit(// limit to 6 of the remaining results
6);
// will perform Map Reduce on _ids 2-7
//transforms Documents into LabelCount objects
List<LabelCount> labelCounts = results.map(new Function<Document, LabelCount>() {
@Override
public LabelCount apply(final Document document) {
return new LabelCount(document.getString("_id"), document.getDouble("value"));
}
}).into(new ArrayList<LabelCount>());
//then
assertThat("Transformed results should still be the same size as original results", labelCounts.size(), is(4));
Collections.sort(labelCounts);
assertThat("Should be LabelCount ordered by count ascending", labelCounts, contains(new LabelCount("d", 1), new LabelCount("a", 2), new LabelCount("c", 4), new LabelCount("b", 5)));
}
use of com.mongodb.Function in project mongo-java-driver by mongodb.
the class MongoCollectionTest method shouldBeAbleToQueryTypedCollectionAndMapResultsIntoTypedLists.
@Test
public void shouldBeAbleToQueryTypedCollectionAndMapResultsIntoTypedLists() {
// given
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new ConcreteCodecProvider()));
MongoCollection<Concrete> collection = database.getCollection(getCollectionName()).withDocumentClass(Concrete.class).withCodecRegistry(codecRegistry).withReadPreference(ReadPreference.primary()).withWriteConcern(WriteConcern.ACKNOWLEDGED);
Concrete firstItem = new Concrete("first", 1, 2L, 3.0, 5L);
collection.insertOne(firstItem);
Concrete secondItem = new Concrete("second", 7, 11L, 13.0, 17L);
collection.insertOne(secondItem);
// when
List<String> listOfStringObjectIds = collection.find(new Document("i", 1)).map(new Function<Concrete, ObjectId>() {
@Override
public ObjectId apply(final Concrete concrete) {
return concrete.getId();
}
}).map(new Function<ObjectId, String>() {
@Override
public String apply(final ObjectId objectId) {
return objectId.toString();
}
}).into(new ArrayList<String>());
// then
assertThat(listOfStringObjectIds.size(), is(1));
assertThat(listOfStringObjectIds.get(0), is(firstItem.getId().toString()));
// when
List<ObjectId> listOfObjectIds = collection.find(new Document("i", 1)).map(new Function<Concrete, ObjectId>() {
@Override
public ObjectId apply(final Concrete concrete) {
return concrete.getId();
}
}).into(new ArrayList<ObjectId>());
// then
assertThat(listOfObjectIds.size(), is(1));
assertThat(listOfObjectIds.get(0), is(firstItem.getId()));
}
Aggregations