use of org.bson.BsonArray 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))))))));
}
use of org.bson.BsonArray 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);
}
use of org.bson.BsonArray 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.BsonArray 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;
}
use of org.bson.BsonArray in project mongo-java-driver by mongodb.
the class QueryProtocol method asFindCommandResponseDocument.
private BsonDocument asFindCommandResponseDocument(final ResponseBuffers responseBuffers, final QueryResult<T> queryResult, final boolean isExplain) {
List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList();
if (responseBuffers.getReplyHeader().getNumberReturned() > 0) {
responseBuffers.getBodyByteBuffer().position(0);
rawResultDocuments = ByteBufBsonDocument.create(responseBuffers);
}
if (isExplain) {
BsonDocument explainCommandResponseDocument = new BsonDocument("ok", new BsonDouble(1));
explainCommandResponseDocument.putAll(rawResultDocuments.get(0));
return explainCommandResponseDocument;
} else {
BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())).append("ns", new BsonString(namespace.getFullName())).append("firstBatch", new BsonArray(rawResultDocuments));
return new BsonDocument("cursor", cursorDocument).append("ok", new BsonDouble(1));
}
}
Aggregations