Search in sources :

Example 1 with DocumentCodec

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

the class ClusterFixture method enableMaxTimeFailPoint.

public static void enableMaxTimeFailPoint() {
    assumeThat(isSharded(), is(false));
    new CommandWriteOperation<BsonDocument>("admin", new BsonDocumentWrapper<Document>(new Document("configureFailPoint", "maxTimeAlwaysTimeOut").append("mode", "alwaysOn"), new DocumentCodec()), new BsonDocumentCodec()).execute(getBinding());
}
Also used : BsonDocument(org.bson.BsonDocument) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BsonDocumentWrapper(org.bson.BsonDocumentWrapper) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 2 with DocumentCodec

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

the class MongoCollectionTest method testAggregationToACollection.

@Test
public void testAggregationToACollection() {
    assumeTrue(serverVersionAtLeast(2, 6));
    // given
    List<Document> documents = asList(new Document("_id", 1), new Document("_id", 2));
    getCollectionHelper().insertDocuments(new DocumentCodec(), documents);
    MongoCollection<Document> collection = database.getCollection(getCollectionName());
    // when
    List<Document> result = collection.aggregate(Collections.singletonList(new Document("$out", "outCollection"))).into(new ArrayList<Document>());
    // then
    assertEquals(documents, result);
}
Also used : DocumentCodec(org.bson.codecs.DocumentCodec) Document(org.bson.Document) Test(org.junit.Test)

Example 3 with DocumentCodec

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

the class CommandMonitoringTest method setUp.

@Before
public void setUp() {
    ServerVersion serverVersion = ClusterFixture.getServerVersion();
    if (definition.containsKey("ignore_if_server_version_less_than")) {
        assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_less_than")) < 0);
    }
    if (definition.containsKey("ignore_if_server_version_greater_than")) {
        assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_greater_than")) > 0);
    }
    if (definition.containsKey("ignore_if_topology_type")) {
        BsonArray topologyTypes = definition.getArray("ignore_if_topology_type");
        for (BsonValue type : topologyTypes) {
            String typeString = type.asString().getValue();
            if (typeString.equals("sharded")) {
                assumeFalse(isSharded());
            } else if (typeString.equals("replica_set")) {
                assumeFalse(isDiscoverableReplicaSet());
            } else if (typeString.equals("standalone")) {
                assumeFalse(isSharded());
            }
        }
    }
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    for (BsonValue document : data) {
        documents.add(document.asDocument());
    }
    CollectionHelper<Document> collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
    collectionHelper.drop();
    collectionHelper.insertDocuments(documents);
    commandListener.reset();
    collection = mongoClient.getDatabase(databaseName).getCollection(collectionName, BsonDocument.class);
    if (definition.getDocument("operation").containsKey("read_preference")) {
        collection = collection.withReadPreference(ReadPreference.valueOf(definition.getDocument("operation").getDocument("read_preference").getString("mode").getValue()));
    }
    helper = new JsonPoweredCrudTestHelper(description, collection);
}
Also used : ServerVersion(com.mongodb.connection.ServerVersion) ArrayList(java.util.ArrayList) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) CollectionHelper(com.mongodb.client.test.CollectionHelper) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 4 with DocumentCodec

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

the class MongoDbBasicConverters method fromInputStreamToDocument.

@Converter
public static Document fromInputStreamToDocument(InputStream is, Exchange exchange) {
    Document answer = null;
    try {
        byte[] input = IOConverter.toBytes(is);
        if (isBson(input)) {
            JsonReader reader = new JsonReader(new String(input));
            DocumentCodec documentReader = new DocumentCodec();
            answer = documentReader.decode(reader, DecoderContext.builder().build());
        } else {
            answer = Document.parse(IOConverter.toString(input, exchange));
        }
    } catch (Exception e) {
        LOG.warn("String -> Document conversion selected, but the following exception occurred. Returning null.", e);
    } finally {
        // we need to make sure to close the input stream
        IOHelper.close(is, "InputStream", LOG);
    }
    return answer;
}
Also used : DocumentCodec(org.bson.codecs.DocumentCodec) JsonReader(org.bson.json.JsonReader) Document(org.bson.Document) FileNotFoundException(java.io.FileNotFoundException) IOConverter(org.apache.camel.converter.IOConverter) Converter(org.apache.camel.Converter)

Example 5 with DocumentCodec

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

Aggregations

Document (org.bson.Document)6 DocumentCodec (org.bson.codecs.DocumentCodec)6 BsonDocument (org.bson.BsonDocument)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 Test (org.junit.Test)2 MongoClient (com.mongodb.MongoClient)1 MongoNamespace (com.mongodb.MongoNamespace)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 CollectionHelper (com.mongodb.client.test.CollectionHelper)1 ServerVersion (com.mongodb.connection.ServerVersion)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Converter (org.apache.camel.Converter)1 IOConverter (org.apache.camel.converter.IOConverter)1 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)1 ExecutionSetupException (org.apache.drill.common.exceptions.ExecutionSetupException)1 PhysicalOperatorSetupException (org.apache.drill.exec.physical.PhysicalOperatorSetupException)1 ScanStats (org.apache.drill.exec.physical.base.ScanStats)1 BsonArray (org.bson.BsonArray)1