Search in sources :

Example 6 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project mongo-java-driver by mongodb.

the class DBObjectCodecTest method shouldEncodeIterableMapAsMap.

@Test
public void shouldEncodeIterableMapAsMap() {
    IterableMap iterableMap = new IterableMap();
    iterableMap.put("first", 1);
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    DBObject doc = new BasicDBObject("map", iterableMap);
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
    assertEquals(new BsonDocument("map", new BsonDocument("first", new BsonInt32(1))), writer.getDocument());
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonInt32(org.bson.BsonInt32) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 7 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project mongo-java-driver by mongodb.

the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnOriginalItemWithDocumentRequiringACustomEncoder.

@Test
public void shouldReplaceAndReturnOriginalItemWithDocumentRequiringACustomEncoder() {
    Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 0);
    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(), 1);
    Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan);
    assertThat("Document, retrieved from getOneAndReplace, should match the document inserted before", returnedDocument, equalTo(pat));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) WorkerCodecProvider(com.mongodb.client.test.WorkerCodecProvider) Worker(com.mongodb.client.test.Worker) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Date(java.util.Date) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 8 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider 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));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) WorkerCodecProvider(com.mongodb.client.test.WorkerCodecProvider) Worker(com.mongodb.client.test.Worker) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Date(java.util.Date) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 9 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider in project camel by apache.

the class MongoDbBasicConverters method fromStringToList.

@Converter
public static List<Bson> fromStringToList(String value) {
    final CodecRegistry codecRegistry = CodecRegistries.fromProviders(Arrays.asList(new ValueCodecProvider(), new BsonValueCodecProvider(), new DocumentCodecProvider()));
    JsonReader reader = new JsonReader(value);
    BsonArrayCodec arrayReader = new BsonArrayCodec(codecRegistry);
    BsonArray docArray = arrayReader.decode(reader, DecoderContext.builder().build());
    List<Bson> answer = new ArrayList<>(docArray.size());
    for (BsonValue doc : docArray) {
        answer.add(doc.asDocument());
    }
    return answer;
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) BsonArray(org.bson.BsonArray) BsonArrayCodec(org.bson.codecs.BsonArrayCodec) ArrayList(java.util.ArrayList) JsonReader(org.bson.json.JsonReader) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Bson(org.bson.conversions.Bson) BsonValue(org.bson.BsonValue) IOConverter(org.apache.camel.converter.IOConverter) Converter(org.apache.camel.Converter)

Example 10 with ValueCodecProvider

use of org.bson.codecs.ValueCodecProvider 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

BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)13 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)13 Test (org.junit.Test)12 DocumentCodecProvider (org.bson.codecs.DocumentCodecProvider)7 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)7 Document (org.bson.Document)6 ObjectId (org.bson.types.ObjectId)5 BsonDocument (org.bson.BsonDocument)4 BsonDocumentWriter (org.bson.BsonDocumentWriter)4 ReturnDocument (com.mongodb.client.model.ReturnDocument)3 Worker (com.mongodb.client.test.Worker)3 WorkerCodecProvider (com.mongodb.client.test.WorkerCodecProvider)3 Date (java.util.Date)3 BsonInt32 (org.bson.BsonInt32)3 BsonArray (org.bson.BsonArray)2 Function (com.mongodb.Function)1 FindOneAndReplaceOptions (com.mongodb.client.model.FindOneAndReplaceOptions)1 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1