Search in sources :

Example 21 with BsonArray

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))))))));
}
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 22 with BsonArray

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);
}
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 23 with BsonArray

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;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonValue(org.bson.BsonValue)

Example 24 with BsonArray

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;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BulkWriteError(com.mongodb.bulk.BulkWriteError) BsonValue(org.bson.BsonValue)

Example 25 with BsonArray

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));
    }
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble)

Aggregations

BsonArray (org.bson.BsonArray)32 BsonDocument (org.bson.BsonDocument)29 BsonString (org.bson.BsonString)17 BsonValue (org.bson.BsonValue)15 ArrayList (java.util.ArrayList)11 BsonInt32 (org.bson.BsonInt32)6 BsonInt64 (org.bson.BsonInt64)6 BsonObjectId (org.bson.BsonObjectId)4 ObjectId (org.bson.types.ObjectId)4 BsonDouble (org.bson.BsonDouble)3 Test (org.junit.Test)3 MongoGridFSException (com.mongodb.MongoGridFSException)2 MongoNamespace (com.mongodb.MongoNamespace)2 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)2 ReadPreferenceServerSelector (com.mongodb.selector.ReadPreferenceServerSelector)2 WritableServerSelector (com.mongodb.selector.WritableServerSelector)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 List (java.util.List)2 Document (org.bson.Document)2