Search in sources :

Example 6 with BsonJavaScript

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));
    }
}
Also used : FindOptions(com.mongodb.client.model.FindOptions) BsonDocument(org.bson.BsonDocument) BsonJavaScript(org.bson.BsonJavaScript)

Example 7 with BsonJavaScript

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;
}
Also used : BsonNull(org.bson.BsonNull) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonBinary(org.bson.BsonBinary) BsonDbPointer(org.bson.BsonDbPointer) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonRegularExpression(org.bson.BsonRegularExpression) BsonBoolean(org.bson.BsonBoolean) BsonObjectId(org.bson.BsonObjectId) BsonJavaScript(org.bson.BsonJavaScript) Date(java.util.Date) BsonTimestamp(org.bson.BsonTimestamp) BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) BsonSymbol(org.bson.BsonSymbol) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble) BsonJavaScriptWithScope(org.bson.BsonJavaScriptWithScope) BsonUndefined(org.bson.BsonUndefined) BsonValue(org.bson.BsonValue)

Example 8 with BsonJavaScript

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());
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonJavaScript(org.bson.BsonJavaScript) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 9 with BsonJavaScript

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);
}
Also used : BsonJavaScript(org.bson.BsonJavaScript)

Example 10 with BsonJavaScript

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);
        }
    }
}
Also used : MapReduceStatistics(com.mongodb.internal.operation.MapReduceStatistics) BsonString(org.bson.BsonString) BsonJavaScript(org.bson.BsonJavaScript) MapReduceToCollectionOperation(com.mongodb.internal.operation.MapReduceToCollectionOperation)

Aggregations

BsonJavaScript (org.bson.BsonJavaScript)10 BsonDocument (org.bson.BsonDocument)6 BsonInt32 (org.bson.BsonInt32)4 Document (org.bson.Document)4 BsonRegularExpression (org.bson.BsonRegularExpression)3 BsonString (org.bson.BsonString)3 FindOptions (com.mongodb.client.model.FindOptions)2 MapReduceStatistics (com.mongodb.internal.operation.MapReduceStatistics)2 MapReduceToCollectionOperation (com.mongodb.internal.operation.MapReduceToCollectionOperation)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Map (java.util.Map)2 BsonArray (org.bson.BsonArray)2 BsonBinary (org.bson.BsonBinary)2 BsonBoolean (org.bson.BsonBoolean)2 BsonDateTime (org.bson.BsonDateTime)2 BsonDbPointer (org.bson.BsonDbPointer)2 BsonDouble (org.bson.BsonDouble)2 BsonInt64 (org.bson.BsonInt64)2 BsonJavaScriptWithScope (org.bson.BsonJavaScriptWithScope)2