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());
}
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);
}
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);
}
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;
}
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));
}
Aggregations