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;
}
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);
}
}
}
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);
}
}
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);
}
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;
}
Aggregations