Search in sources :

Example 96 with BsonDocument

use of org.bson.BsonDocument 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 {
        MapReduceToCollectionOperation operation = createMapReduceToCollectionOperation();
        String dbName = databaseName != null ? databaseName : namespace.getDatabaseName();
        MongoIterable<TResult> delegated = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(dbName, collectionName), documentClass, resultClass, codecRegistry, primary(), readConcern, executor, new BsonDocument(), new FindOptions().collation(collation)).batchSize(batchSize);
        return new AwaitingWriteOperationIterable<TResult, MapReduceStatistics>(operation, executor, delegated);
    }
}
Also used : FindOptions(com.mongodb.client.model.FindOptions) MongoNamespace(com.mongodb.MongoNamespace) BsonJavaScript(org.bson.BsonJavaScript) MapReduceToCollectionOperation(com.mongodb.operation.MapReduceToCollectionOperation) BsonDocument(org.bson.BsonDocument)

Example 97 with BsonDocument

use of org.bson.BsonDocument 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 98 with BsonDocument

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

the class CollectionAcceptanceTest method shouldBeAbleToUseBsonValueToDistinctDocumentsOfVaryingTypes.

@SuppressWarnings("unchecked")
@Test
public void shouldBeAbleToUseBsonValueToDistinctDocumentsOfVaryingTypes() {
    List<Object> mixedList = new ArrayList<Object>();
    mixedList.add(2);
    mixedList.add("d");
    mixedList.add(new Document("e", 3));
    collection.drop();
    collection.insertMany(asList(new Document("id", "a"), new Document("id", 1), new Document("id", new Document("b", "c")), new Document("id", new Document("list", mixedList))));
    List<BsonValue> distinct = collection.distinct("id", BsonValue.class).into(new ArrayList<BsonValue>());
    assertTrue(distinct.containsAll(asList(new BsonString("a"), new BsonInt32(1), new BsonDocument("b", new BsonString("c")), new BsonDocument("list", new BsonArray(asList(new BsonInt32(2), new BsonString("d"), new BsonDocument("e", new BsonInt32(3))))))));
    distinct = collection.distinct("id", new Document("id", new Document("$ne", 1)), BsonValue.class).into(new ArrayList<BsonValue>());
    assertTrue(distinct.containsAll(asList(new BsonString("a"), new BsonDocument("b", new BsonString("c")), new BsonDocument("list", new BsonArray(asList(new BsonInt32(2), new BsonString("d"), new BsonDocument("e", new BsonInt32(3))))))));
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 99 with BsonDocument

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

the class BsonDocumentCodec method decode.

@Override
public BsonDocument decode(final BsonReader reader, final DecoderContext decoderContext) {
    List<BsonElement> keyValuePairs = new ArrayList<BsonElement>();
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String fieldName = reader.readName();
        keyValuePairs.add(new BsonElement(fieldName, readValue(reader, decoderContext)));
    }
    reader.readEndDocument();
    return new BsonDocument(keyValuePairs);
}
Also used : BsonElement(org.bson.BsonElement) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList)

Example 100 with BsonDocument

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

the class BsonJavaScriptWithScopeCodec method decode.

@Override
public BsonJavaScriptWithScope decode(final BsonReader bsonReader, final DecoderContext decoderContext) {
    String code = bsonReader.readJavaScriptWithScope();
    BsonDocument scope = documentCodec.decode(bsonReader, decoderContext);
    return new BsonJavaScriptWithScope(code, scope);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonJavaScriptWithScope(org.bson.BsonJavaScriptWithScope)

Aggregations

BsonDocument (org.bson.BsonDocument)166 BsonString (org.bson.BsonString)53 BsonValue (org.bson.BsonValue)37 Test (org.junit.Test)34 BsonArray (org.bson.BsonArray)29 BsonInt32 (org.bson.BsonInt32)28 ArrayList (java.util.ArrayList)24 BsonDocumentReader (org.bson.BsonDocumentReader)17 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)14 BsonDocumentWriter (org.bson.BsonDocumentWriter)14 BsonInt64 (org.bson.BsonInt64)14 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 BsonDouble (org.bson.BsonDouble)8 MongoNamespace (com.mongodb.MongoNamespace)6 Before (org.junit.Before)6 MongoGridFSException (com.mongodb.MongoGridFSException)5 BsonObjectId (org.bson.BsonObjectId)5 BsonWriter (org.bson.BsonWriter)5 Document (org.bson.Document)5 ObjectId (org.bson.types.ObjectId)5