Search in sources :

Example 6 with CodecRegistry

use of org.bson.codecs.configuration.CodecRegistry 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 7 with CodecRegistry

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

the class Updates method pullByFilter.

/**
     * Creates an update that removes from an array all elements that match the given filter.
     *
     * @param filter the query filter
     * @return the update
     * @mongodb.driver.manual reference/operator/update/pull/ $pull
     */
public static Bson pullByFilter(final Bson filter) {
    return new Bson() {

        @Override
        public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> tDocumentClass, final CodecRegistry codecRegistry) {
            BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
            writer.writeStartDocument();
            writer.writeName("$pull");
            encodeValue(writer, filter, codecRegistry);
            writer.writeEndDocument();
            return writer.getDocument();
        }
    };
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Bson(org.bson.conversions.Bson)

Example 8 with CodecRegistry

use of org.bson.codecs.configuration.CodecRegistry 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));
}
Also used : DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) DocumentCodec(org.bson.codecs.DocumentCodec) Document(org.bson.Document) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Test(org.junit.Test)

Example 9 with CodecRegistry

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

CodecRegistry (org.bson.codecs.configuration.CodecRegistry)9 DocumentCodecProvider (org.bson.codecs.DocumentCodecProvider)8 Document (org.bson.Document)7 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)7 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)7 Test (org.junit.Test)7 ObjectId (org.bson.types.ObjectId)5 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 Bson (org.bson.conversions.Bson)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 Converter (org.apache.camel.Converter)1 IOConverter (org.apache.camel.converter.IOConverter)1 BsonArray (org.bson.BsonArray)1 BsonDocument (org.bson.BsonDocument)1