Search in sources :

Example 26 with BsonValue

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

the class UserOperationHelper method asCommandDocument.

static BsonDocument asCommandDocument(final MongoCredential credential, final boolean readOnly, final String commandName) {
    BsonDocument document = new BsonDocument();
    document.put(commandName, new BsonString(credential.getUserName()));
    document.put("pwd", new BsonString(createAuthenticationHash(credential.getUserName(), credential.getPassword())));
    document.put("digestPassword", BsonBoolean.FALSE);
    document.put("roles", new BsonArray(Arrays.<BsonValue>asList(new BsonString(getRoleName(credential, readOnly)))));
    return document;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonValue(org.bson.BsonValue)

Example 27 with BsonValue

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

the class ConnectionStringTest method testValidOptions.

private void testValidOptions() {
    ConnectionString connectionString = null;
    try {
        connectionString = new ConnectionString(input);
    } catch (Throwable t) {
        assertTrue(String.format("Connection string '%s' should not have throw an exception: %s", input, t.toString()), false);
    }
    for (Map.Entry<String, BsonValue> option : definition.getDocument("options").entrySet()) {
        if (option.getKey().equals("authmechanism")) {
            String expected = option.getValue().asString().getValue();
            String actual = connectionString.getCredentialList().get(0).getAuthenticationMechanism().getMechanismName();
            assertEquals(expected, actual);
        } else if (option.getKey().equals("replicaset")) {
            String expected = option.getValue().asString().getValue();
            assertEquals(expected, connectionString.getRequiredReplicaSetName());
        } else if (option.getKey().equals("wtimeoutms")) {
            int expected = option.getValue().asInt32().getValue();
            assertEquals(expected, connectionString.getWriteConcern().getWTimeout(TimeUnit.MILLISECONDS).intValue());
        } else {
            assertTrue(String.format("Unsupported option '%s' in '%s'", option.getKey(), input), false);
        }
    }
}
Also used : ConnectionString(com.mongodb.ConnectionString) ConnectionString(com.mongodb.ConnectionString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 28 with BsonValue

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

the class AggregateIterableImpl method execute.

@SuppressWarnings("deprecation")
private MongoIterable<TResult> execute() {
    List<BsonDocument> aggregateList = createBsonDocumentList(pipeline);
    BsonValue outCollection = getOutCollection(aggregateList);
    if (outCollection != null) {
        executor.execute(createAggregateToCollectionOperation(aggregateList));
        FindIterable<TResult> findOperation = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(namespace.getDatabaseName(), outCollection.asString().getValue()), documentClass, resultClass, codecRegistry, readPreference, readConcern, executor, new BsonDocument(), new FindOptions().collation(collation));
        if (batchSize != null) {
            findOperation.batchSize(batchSize);
        }
        return findOperation;
    } else {
        return new OperationIterable<TResult>(new AggregateOperation<TResult>(namespace, aggregateList, codecRegistry.get(resultClass)).maxTime(maxTimeMS, MILLISECONDS).allowDiskUse(allowDiskUse).batchSize(batchSize).useCursor(useCursor).readConcern(readConcern).collation(collation), readPreference, executor);
    }
}
Also used : FindOptions(com.mongodb.client.model.FindOptions) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue)

Example 29 with BsonValue

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

the class DBObjectCodec method getDocumentId.

@Override
public BsonValue getDocumentId(final DBObject document) {
    if (!documentHasId(document)) {
        throw new IllegalStateException("The document does not contain an _id");
    }
    Object id = document.get(ID_FIELD_NAME);
    if (id instanceof BsonValue) {
        return (BsonValue) id;
    }
    BsonDocument idHoldingDocument = new BsonDocument();
    BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
    writer.writeStartDocument();
    writer.writeName(ID_FIELD_NAME);
    writeValue(writer, EncoderContext.builder().build(), id);
    writer.writeEndDocument();
    return idHoldingDocument.get(ID_FIELD_NAME);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonWriter(org.bson.BsonWriter) BSONObject(org.bson.BSONObject) BsonValue(org.bson.BsonValue)

Example 30 with BsonValue

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

the class WriteCommandResultHelper method getWriteErrors.

@SuppressWarnings("unchecked")
private static List<BulkWriteError> getWriteErrors(final BsonDocument result) {
    List<BulkWriteError> writeErrors = new ArrayList<BulkWriteError>();
    BsonArray writeErrorsDocuments = (BsonArray) result.get("writeErrors");
    if (writeErrorsDocuments != null) {
        for (BsonValue cur : writeErrorsDocuments) {
            BsonDocument curDocument = (BsonDocument) cur;
            writeErrors.add(new BulkWriteError(curDocument.getNumber("code").intValue(), curDocument.getString("errmsg").getValue(), curDocument.getDocument("errInfo", new BsonDocument()), curDocument.getNumber("index").intValue()));
        }
    }
    return writeErrors;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BulkWriteError(com.mongodb.bulk.BulkWriteError) BsonValue(org.bson.BsonValue)

Aggregations

BsonValue (org.bson.BsonValue)47 BsonDocument (org.bson.BsonDocument)37 ArrayList (java.util.ArrayList)17 BsonArray (org.bson.BsonArray)15 BsonString (org.bson.BsonString)15 BsonInt32 (org.bson.BsonInt32)6 Test (org.junit.Test)5 BsonObjectId (org.bson.BsonObjectId)4 MongoNamespace (com.mongodb.MongoNamespace)3 AggregateToCollectionOperation (com.mongodb.operation.AggregateToCollectionOperation)3 List (java.util.List)3 BsonInt64 (org.bson.BsonInt64)3 BsonNumber (org.bson.BsonNumber)3 Document (org.bson.Document)3 ObjectId (org.bson.types.ObjectId)3 Before (org.junit.Before)3 ConnectionString (com.mongodb.ConnectionString)2 MongoGridFSException (com.mongodb.MongoGridFSException)2 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)2 FindOptions (com.mongodb.client.model.FindOptions)2