use of org.bson.BsonJavaScript in project mongo-java-driver by mongodb.
the class MapReduceIterableImpl method execute.
MongoIterable<TResult> execute() {
if (inline) {
MapReduceWithInlineResultsOperation<TResult> operation = new MapReduceWithInlineResultsOperation<TResult>(namespace, new BsonJavaScript(mapFunction), new BsonJavaScript(reduceFunction), codecRegistry.get(resultClass)).filter(toBsonDocument(filter)).limit(limit).maxTime(maxTimeMS, MILLISECONDS).jsMode(jsMode).scope(toBsonDocument(scope)).sort(toBsonDocument(sort)).verbose(verbose).readConcern(readConcern).collation(collation);
if (finalizeFunction != null) {
operation.finalizeFunction(new BsonJavaScript(finalizeFunction));
}
return new OperationIterable<TResult>(operation, readPreference, executor);
} else {
executor.execute(createMapReduceToCollectionOperation());
String dbName = databaseName != null ? databaseName : namespace.getDatabaseName();
return new FindIterableImpl<TDocument, TResult>(new MongoNamespace(dbName, collectionName), documentClass, resultClass, codecRegistry, primary(), readConcern, executor, new BsonDocument(), new FindOptions().collation(collation).batchSize(batchSize));
}
}
use of org.bson.BsonJavaScript in project pinpoint by naver.
the class MongoDBITBase method createComplexDocument.
private Document createComplexDocument() {
// insert Data
BsonValue a = new BsonString("stest");
BsonValue b = new BsonDouble(111);
BsonValue c = new BsonBoolean(true);
Document document = new Document().append("int32", new BsonInt32(12)).append("int64", new BsonInt64(77L)).append("bo\"olean", new BsonBoolean(true)).append("date", new BsonDateTime(new Date().getTime())).append("double", new BsonDouble(12.3)).append("string", new BsonString("pinpoint")).append("objectId", new BsonObjectId(new ObjectId())).append("code", new BsonJavaScript("int i = 10;")).append("codeWithScope", new BsonJavaScriptWithScope("int x = y", new BsonDocument("y", new BsonInt32(1)))).append("regex", new BsonRegularExpression("^test.*regex.*xyz$", "big")).append("symbol", new BsonSymbol("wow")).append("timestamp", new BsonTimestamp(0x12345678, 5)).append("undefined", new BsonUndefined()).append("binary1", new BsonBinary(new byte[] { (byte) 0xe0, 0x4f, (byte) 0xd0, 0x20 })).append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[] { 1, 1, 1, 1, 1 })).append("arrayInt", new BsonArray(Arrays.asList(a, b, c, new BsonInt32(7)))).append("document", new BsonDocument("a", new BsonInt32(77))).append("dbPointer", new BsonDbPointer("db.coll", new ObjectId())).append("null", new BsonNull());
return document;
}
use of org.bson.BsonJavaScript in project mongo-java-driver by mongodb.
the class MapReducePublisherImplTest method shouldBuildTheExpectedMapReduceWithInlineResultsOperation.
@DisplayName("Should build the expected MapReduceWithInlineResultsOperation")
@Test
void shouldBuildTheExpectedMapReduceWithInlineResultsOperation() {
configureBatchCursor();
TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor()));
com.mongodb.reactivestreams.client.MapReducePublisher<Document> publisher = new MapReducePublisherImpl<>(null, createMongoOperationPublisher(executor), MAP_FUNCTION, REDUCE_FUNCTION);
MapReduceWithInlineResultsOperation<Document> expectedOperation = new MapReduceWithInlineResultsOperation<>(NAMESPACE, new BsonJavaScript(MAP_FUNCTION), new BsonJavaScript(REDUCE_FUNCTION), getDefaultCodecRegistry().get(Document.class)).verbose(true);
// default input should be as expected
Flux.from(publisher).blockFirst();
MapReducePublisherImpl.WrappedMapReduceReadOperation operation = (MapReducePublisherImpl.WrappedMapReduceReadOperation) executor.getReadOperation();
assertNotNull(operation);
assertOperationIsTheSameAs(expectedOperation, operation.getOperation());
assertEquals(ReadPreference.primary(), executor.getReadPreference());
// Should apply settings
publisher.batchSize(100).bypassDocumentValidation(true).collation(COLLATION).filter(new Document("filter", 1)).finalizeFunction(FINALIZE_FUNCTION).limit(999).maxTime(10, SECONDS).scope(new Document("scope", 1)).sort(Sorts.ascending("sort")).verbose(false);
expectedOperation.collation(COLLATION).collation(COLLATION).filter(BsonDocument.parse("{filter: 1}")).finalizeFunction(new BsonJavaScript(FINALIZE_FUNCTION)).limit(999).maxTime(10, SECONDS).maxTime(10, SECONDS).scope(new BsonDocument("scope", new BsonInt32(1))).sort(new BsonDocument("sort", new BsonInt32(1))).verbose(false);
configureBatchCursor();
Flux.from(publisher).blockFirst();
operation = (MapReducePublisherImpl.WrappedMapReduceReadOperation) executor.getReadOperation();
assertNotNull(operation);
assertOperationIsTheSameAs(expectedOperation, operation.getOperation());
assertEquals(ReadPreference.primary(), executor.getReadPreference());
}
use of org.bson.BsonJavaScript in project mongo-java-driver by mongodb.
the class IdHoldingBsonWriter method writeJavaScript.
@Override
public void writeJavaScript(final String code) {
addBsonValue(() -> new BsonJavaScript(code), () -> getIdBsonWriter().writeJavaScript(code));
super.writeJavaScript(code);
}
use of org.bson.BsonJavaScript in project mongo-java-driver by mongodb.
the class DBCollection method mapReduce.
/**
* Allows you to run map-reduce aggregation operations over a collection.
*
* @param command specifies the details of the Map Reduce operation to perform
* @return a MapReduceOutput containing the results of the map reduce operation
* @mongodb.driver.manual core/map-reduce/ Map-Reduce
*/
public MapReduceOutput mapReduce(final MapReduceCommand command) {
ReadPreference readPreference = command.getReadPreference() == null ? getReadPreference() : command.getReadPreference();
Map<String, Object> scope = command.getScope();
Boolean jsMode = command.getJsMode();
if (command.getOutputType() == MapReduceCommand.OutputType.INLINE) {
MapReduceWithInlineResultsOperation<DBObject> operation = new MapReduceWithInlineResultsOperation<DBObject>(getNamespace(), new BsonJavaScript(command.getMap()), new BsonJavaScript(command.getReduce()), getDefaultDBObjectCodec()).filter(wrapAllowNull(command.getQuery())).limit(command.getLimit()).maxTime(command.getMaxTime(MILLISECONDS), MILLISECONDS).jsMode(jsMode == null ? false : jsMode).sort(wrapAllowNull(command.getSort())).verbose(command.isVerbose()).collation(command.getCollation());
if (scope != null) {
operation.scope(wrap(new BasicDBObject(scope)));
}
if (command.getFinalize() != null) {
operation.finalizeFunction(new BsonJavaScript(command.getFinalize()));
}
MapReduceBatchCursor<DBObject> executionResult = executor.execute(operation, readPreference, getReadConcern());
return new MapReduceOutput(command.toDBObject(), executionResult);
} else {
String action;
switch(command.getOutputType()) {
case REPLACE:
action = "replace";
break;
case MERGE:
action = "merge";
break;
case REDUCE:
action = "reduce";
break;
default:
throw new IllegalArgumentException("Unexpected output type");
}
MapReduceToCollectionOperation operation = new MapReduceToCollectionOperation(getNamespace(), new BsonJavaScript(command.getMap()), new BsonJavaScript(command.getReduce()), command.getOutputTarget(), getWriteConcern()).filter(wrapAllowNull(command.getQuery())).limit(command.getLimit()).maxTime(command.getMaxTime(MILLISECONDS), MILLISECONDS).jsMode(jsMode == null ? false : jsMode).sort(wrapAllowNull(command.getSort())).verbose(command.isVerbose()).action(action).databaseName(command.getOutputDB()).bypassDocumentValidation(command.getBypassDocumentValidation()).collation(command.getCollation());
if (scope != null) {
operation.scope(wrap(new BasicDBObject(scope)));
}
if (command.getFinalize() != null) {
operation.finalizeFunction(new BsonJavaScript(command.getFinalize()));
}
try {
MapReduceStatistics mapReduceStatistics = executor.execute(operation, getReadConcern());
DBCollection mapReduceOutputCollection = getMapReduceOutputCollection(command);
DBCursor executionResult = mapReduceOutputCollection.find();
return new MapReduceOutput(command.toDBObject(), executionResult, mapReduceStatistics, mapReduceOutputCollection);
} catch (MongoWriteConcernException e) {
throw createWriteConcernException(e);
}
}
}
Aggregations