Search in sources :

Example 21 with BsonValue

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

the class MongoCollectionImpl method toUpdateResult.

private UpdateResult toUpdateResult(final com.mongodb.bulk.BulkWriteResult result) {
    if (result.wasAcknowledged()) {
        Long modifiedCount = result.isModifiedCountAvailable() ? (long) result.getModifiedCount() : null;
        BsonValue upsertedId = result.getUpserts().isEmpty() ? null : result.getUpserts().get(0).getId();
        return UpdateResult.acknowledged(result.getMatchedCount(), modifiedCount, upsertedId);
    } else {
        return UpdateResult.unacknowledged();
    }
}
Also used : BsonValue(org.bson.BsonValue)

Example 22 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();
    BsonValue outCollection = getAggregateOutCollection(aggregateList);
    if (outCollection != null) {
        AggregateToCollectionOperation operation = new AggregateToCollectionOperation(namespace, aggregateList, writeConcern).maxTime(maxTimeMS, MILLISECONDS).allowDiskUse(allowDiskUse).bypassDocumentValidation(bypassDocumentValidation).collation(collation);
        MongoIterable<TResult> delegated = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(namespace.getDatabaseName(), outCollection.asString().getValue()), documentClass, resultClass, codecRegistry, primary(), readConcern, executor, new BsonDocument(), new FindOptions().collation(collation));
        if (batchSize != null) {
            delegated.batchSize(batchSize);
        }
        return new AwaitingWriteOperationIterable<TResult, Void>(operation, executor, delegated);
    } 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 : AggregateToCollectionOperation(com.mongodb.operation.AggregateToCollectionOperation) FindOptions(com.mongodb.client.model.FindOptions) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue)

Example 23 with BsonValue

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

the class AggregateIterableImpl method toCollection.

@Override
public void toCollection(final SingleResultCallback<Void> callback) {
    List<BsonDocument> aggregateList = createBsonDocumentList();
    BsonValue outCollection = getAggregateOutCollection(aggregateList);
    if (outCollection == null) {
        throw new IllegalStateException("The last stage of the aggregation pipeline must be $out");
    }
    executor.execute(new AggregateToCollectionOperation(namespace, aggregateList, writeConcern).maxTime(maxTimeMS, MILLISECONDS).allowDiskUse(allowDiskUse).collation(collation), callback);
}
Also used : AggregateToCollectionOperation(com.mongodb.operation.AggregateToCollectionOperation) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue)

Example 24 with BsonValue

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

the class MongoCollectionImpl method toUpdateResult.

private UpdateResult toUpdateResult(final com.mongodb.bulk.BulkWriteResult result) {
    if (result.wasAcknowledged()) {
        Long modifiedCount = result.isModifiedCountAvailable() ? (long) result.getModifiedCount() : null;
        BsonValue upsertedId = result.getUpserts().isEmpty() ? null : result.getUpserts().get(0).getId();
        return UpdateResult.acknowledged(result.getMatchedCount(), modifiedCount, upsertedId);
    } else {
        return UpdateResult.unacknowledged();
    }
}
Also used : BsonValue(org.bson.BsonValue)

Example 25 with BsonValue

use of org.bson.BsonValue 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)

Aggregations

BsonValue (org.bson.BsonValue)45 BsonDocument (org.bson.BsonDocument)37 ArrayList (java.util.ArrayList)17 BsonArray (org.bson.BsonArray)15 BsonString (org.bson.BsonString)14 BsonInt32 (org.bson.BsonInt32)6 BsonObjectId (org.bson.BsonObjectId)4 Test (org.junit.Test)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 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 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)2