Search in sources :

Example 1 with Function

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)));
}
Also used : Function(com.mongodb.Function) Document(org.bson.Document) Test(org.junit.Test)

Example 2 with Function

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()));
}
Also used : ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Function(com.mongodb.Function) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Aggregations

Function (com.mongodb.Function)2 Document (org.bson.Document)2 Test (org.junit.Test)2 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)1 DocumentCodecProvider (org.bson.codecs.DocumentCodecProvider)1 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)1 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)1 ObjectId (org.bson.types.ObjectId)1