Search in sources :

Example 81 with BsonDocument

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

the class DBCollection method distinct.

/**
     * Find the distinct values for a specified field across a collection and returns the results in an array.
     *
     * @param fieldName  Specifies the field for which to return the distinct values
     * @param options    the options to apply for this operation
     * @return A {@code List} of the distinct values
     * @mongodb.driver.manual reference/command/distinct Distinct Command
     * @since 3.4
     */
@SuppressWarnings("unchecked")
public List distinct(final String fieldName, final DBCollectionDistinctOptions options) {
    notNull("fieldName", fieldName);
    return new OperationIterable<BsonValue>(new DistinctOperation<BsonValue>(getNamespace(), fieldName, new BsonValueCodec()).readConcern(options.getReadConcern() != null ? options.getReadConcern() : getReadConcern()).filter(wrapAllowNull(options.getFilter())).collation(options.getCollation()), options.getReadPreference() != null ? options.getReadPreference() : getReadPreference(), executor).map(new Function<BsonValue, Object>() {

        @Override
        public Object apply(final BsonValue bsonValue) {
            if (bsonValue == null) {
                return null;
            }
            BsonDocument document = new BsonDocument("value", bsonValue);
            DBObject obj = getDefaultDBObjectCodec().decode(new BsonDocumentReader(document), DecoderContext.builder().build());
            return obj.get("value");
        }
    }).into(new ArrayList<Object>());
}
Also used : DistinctOperation(com.mongodb.operation.DistinctOperation) BsonValueCodec(org.bson.codecs.BsonValueCodec) BsonDocument(org.bson.BsonDocument) BsonDocumentReader(org.bson.BsonDocumentReader) BsonValue(org.bson.BsonValue)

Example 82 with BsonDocument

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

the class WriteConcernConnectionStringTest method getExpectedWriteConcern.

private WriteConcern getExpectedWriteConcern() {
    BsonDocument writeConcernDocument = definition.getDocument("writeConcern");
    BsonValue wValue = writeConcernDocument.get("w");
    WriteConcern retVal;
    if (wValue == null) {
        retVal = WriteConcern.ACKNOWLEDGED;
    } else if (wValue instanceof BsonNumber) {
        retVal = new WriteConcern(wValue.asNumber().intValue());
    } else if (wValue instanceof BsonString) {
        retVal = new WriteConcern(wValue.asString().getValue());
    } else {
        throw new IllegalArgumentException("Unexpected w value: " + wValue);
    }
    if (writeConcernDocument.containsKey("wtimeoutMS")) {
        retVal = retVal.withWTimeout(writeConcernDocument.getNumber("wtimeoutMS", new BsonInt32(0)).intValue(), TimeUnit.MILLISECONDS);
    }
    if (writeConcernDocument.containsKey("journal")) {
        retVal = retVal.withJournal(writeConcernDocument.getBoolean("journal", BsonBoolean.FALSE).getValue());
    }
    return retVal;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonNumber(org.bson.BsonNumber) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 83 with BsonDocument

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

the class AbstractServerDiscoveryAndMonitoringTest method applyResponse.

protected void applyResponse(final BsonArray response) {
    ServerAddress serverAddress = new ServerAddress(response.get(0).asString().getValue());
    BsonDocument isMasterResult = response.get(1).asDocument();
    ServerDescription serverDescription;
    if (isMasterResult.isEmpty()) {
        serverDescription = ServerDescription.builder().type(ServerType.UNKNOWN).state(CONNECTING).address(serverAddress).build();
    } else {
        serverDescription = createServerDescription(serverAddress, isMasterResult, getVersion(new BsonDocument("versionArray", new BsonArray(asList(new BsonInt32(2), new BsonInt32(6), new BsonInt32(0))))), 5000000);
    }
    factory.sendNotification(serverAddress, serverDescription);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) DescriptionHelper.createServerDescription(com.mongodb.connection.DescriptionHelper.createServerDescription) BsonArray(org.bson.BsonArray) ServerAddress(com.mongodb.ServerAddress)

Example 84 with BsonDocument

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

the class MaxDocumentSizeTest method setUp.

@Before
public void setUp() {
    message = new InsertMessage("test.test", true, ACKNOWLEDGED, asList(new InsertRequest(new BsonDocument("bytes", new BsonBinary(new byte[2048])))), MessageSettings.builder().maxDocumentSize(1024).build());
    buffer = new ByteBufferBsonOutput(new SimpleBufferProvider());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) InsertRequest(com.mongodb.bulk.InsertRequest) Before(org.junit.Before)

Example 85 with BsonDocument

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

the class MaxMessageSizeTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() {
    BsonBinary binary = new BsonBinary(new byte[2048]);
    message = new InsertMessage("test.test", true, ACKNOWLEDGED, Arrays.asList(new InsertRequest(new BsonDocument("bytes", binary)), new InsertRequest(new BsonDocument("bytes", binary)), new InsertRequest(new BsonDocument("bytes", binary))), MessageSettings.builder().maxMessageSize(4500).build());
    buffer = new ByteBufferBsonOutput(new SimpleBufferProvider());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) InsertRequest(com.mongodb.bulk.InsertRequest) Before(org.junit.Before)

Aggregations

BsonDocument (org.bson.BsonDocument)169 BsonString (org.bson.BsonString)53 BsonValue (org.bson.BsonValue)37 Test (org.junit.Test)36 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 Document (org.bson.Document)7 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 ObjectId (org.bson.types.ObjectId)5