Search in sources :

Example 11 with BsonInt32

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

the class CommandResultTest method testNullErrorCode.

@Test
public void testNullErrorCode() throws UnknownHostException {
    try {
        new CommandResult(new BsonDocument("ok", new BsonInt32(0)), new ServerAddress()).throwOnError();
        fail("Should throw");
    } catch (MongoCommandException e) {
        assertEquals(-1, e.getCode());
    }
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 12 with BsonInt32

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

the class CrudTest method toResult.

private BsonDocument toResult(final MongoOperationUpdateResult operation) {
    UpdateResult updateResult = operation.get();
    BsonDocument resultDoc = new BsonDocument("matchedCount", new BsonInt32((int) updateResult.getMatchedCount()));
    if (updateResult.isModifiedCountAvailable()) {
        resultDoc.append("modifiedCount", new BsonInt32((int) updateResult.getModifiedCount()));
    }
    // in replaceOne-pre_2.6
    if (updateResult.getUpsertedId() != null && !updateResult.getUpsertedId().isObjectId()) {
        resultDoc.append("upsertedId", updateResult.getUpsertedId());
    }
    resultDoc.append("upsertedCount", updateResult.getUpsertedId() == null ? new BsonInt32(0) : new BsonInt32(1));
    return toResult(resultDoc);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 13 with BsonInt32

use of org.bson.BsonInt32 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 14 with BsonInt32

use of org.bson.BsonInt32 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 15 with BsonInt32

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

the class WriteConcernDocumentTest method getWriteConcern.

private WriteConcern getWriteConcern(final BsonDocument writeConcernDocument) {
    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) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Aggregations

BsonInt32 (org.bson.BsonInt32)32 BsonDocument (org.bson.BsonDocument)28 BsonString (org.bson.BsonString)18 Test (org.junit.Test)8 BsonArray (org.bson.BsonArray)6 BsonInt64 (org.bson.BsonInt64)6 BsonValue (org.bson.BsonValue)6 Document (org.bson.Document)3 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)3 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)3 HashMap (java.util.HashMap)2 BsonDocumentWriter (org.bson.BsonDocumentWriter)2 BsonDouble (org.bson.BsonDouble)2 BsonNumber (org.bson.BsonNumber)2 ServerAddress (com.mongodb.ServerAddress)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 UpdateRequest (com.mongodb.bulk.UpdateRequest)1 UpdateResult (com.mongodb.client.result.UpdateResult)1 DescriptionHelper.createServerDescription (com.mongodb.connection.DescriptionHelper.createServerDescription)1 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)1