use of org.bson.codecs.DocumentCodecProvider in project mongo-java-driver by mongodb.
the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder.
@Test
public void shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder() {
Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 3);
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
collection.insertOne(pat);
assertThat(collection.count(), is(1L));
Worker jordan = new Worker(pat.getId(), "Jordan", "Engineer", new Date(), 7);
Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan, new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER));
assertThat("Worker retrieved from replaceOneAndGet should match the updated Worker", returnedDocument, equalTo(jordan));
}
use of org.bson.codecs.DocumentCodecProvider in project mongo-java-driver by mongodb.
the class MongoCollectionTest method testMapReduceWithGenerics.
@Test
public void testMapReduceWithGenerics() {
// given
CodecRegistry codecRegistry = fromProviders(asList(new DocumentCodecProvider(), new NameCodecProvider()));
getCollectionHelper().insertDocuments(new DocumentCodec(), new Document("name", "Pete").append("job", "handyman"), new Document("name", "Sam").append("job", "Plumber"), new Document("name", "Pete").append("job", "'electrician'"));
String mapFunction = "function(){ emit( this.name , 1 ); }";
String reduceFunction = "function(key, values){ return values.length; }";
MongoCollection<Document> collection = database.getCollection(getCollectionName()).withCodecRegistry(codecRegistry).withReadPreference(ReadPreference.primary()).withWriteConcern(WriteConcern.ACKNOWLEDGED);
// when
List<Name> result = collection.mapReduce(mapFunction, reduceFunction, Name.class).into(new ArrayList<Name>());
// then
assertEquals(new Name("Pete", 2), result.get(0));
assertEquals(new Name("Sam", 1), result.get(1));
}
use of org.bson.codecs.DocumentCodecProvider 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