Search in sources :

Example 11 with BsonValue

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

the class DBCollection method aggregate.

@SuppressWarnings("deprecation")
private Cursor aggregate(final List<? extends DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference, final boolean returnCursorForOutCollection) {
    if (options == null) {
        throw new IllegalArgumentException("options can not be null");
    }
    List<BsonDocument> stages = preparePipeline(pipeline);
    BsonValue outCollection = stages.get(stages.size() - 1).get("$out");
    if (outCollection != null) {
        AggregateToCollectionOperation operation = new AggregateToCollectionOperation(getNamespace(), stages, getWriteConcern()).maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS).allowDiskUse(options.getAllowDiskUse()).bypassDocumentValidation(options.getBypassDocumentValidation()).collation(options.getCollation());
        try {
            executor.execute(operation);
            if (returnCursorForOutCollection) {
                return new DBCursor(database.getCollection(outCollection.asString().getValue()), new BasicDBObject(), new DBCollectionFindOptions().readPreference(primary()).collation(options.getCollation()));
            } else {
                return null;
            }
        } catch (MongoWriteConcernException e) {
            throw createWriteConcernException(e);
        }
    } else {
        AggregateOperation<DBObject> operation = new AggregateOperation<DBObject>(getNamespace(), stages, getDefaultDBObjectCodec()).readConcern(getReadConcern()).maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS).allowDiskUse(options.getAllowDiskUse()).batchSize(options.getBatchSize()).useCursor(options.getOutputMode() == com.mongodb.AggregationOptions.OutputMode.CURSOR).collation(options.getCollation());
        BatchCursor<DBObject> cursor = executor.execute(operation, readPreference);
        return new MongoCursorAdapter(new MongoBatchCursorAdapter<DBObject>(cursor));
    }
}
Also used : AggregateToCollectionOperation(com.mongodb.operation.AggregateToCollectionOperation) DBCollectionFindOptions(com.mongodb.client.model.DBCollectionFindOptions) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) AggregateOperation(com.mongodb.operation.AggregateOperation)

Example 12 with BsonValue

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

the class ServerDiscoveryAndMonitoringTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    for (BsonValue phase : getDefinition().getArray("phases")) {
        for (BsonValue response : phase.asDocument().getArray("responses")) {
            applyResponse(response.asArray());
        }
        BsonDocument outcome = phase.asDocument().getDocument("outcome");
        assertTopologyType(outcome.getString("topologyType").getValue());
        assertServers(outcome.getDocument("servers"));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 13 with BsonValue

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

the class ServerSelectionRttTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    ExponentiallyWeightedMovingAverage subject = new ExponentiallyWeightedMovingAverage(0.2);
    BsonValue current = definition.get("avg_rtt_ms");
    if (current.isNumber()) {
        subject.addSample(current.asNumber().longValue());
    }
    subject.addSample(definition.getNumber("new_rtt_ms").longValue());
    long expected = definition.getNumber("new_avg_rtt").asNumber().longValue();
    assertEquals(subject.getAverage(), expected);
}
Also used : BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 14 with BsonValue

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

the class CrudTest method setUp.

@Before
public void setUp() {
    super.setUp();
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    for (BsonValue document : data) {
        documents.add(document.asDocument());
    }
    getCollectionHelper().insertDocuments(documents);
    collection = database.getCollection(getClass().getName(), BsonDocument.class);
    helper = new JsonPoweredCrudTestHelper(description, collection);
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 15 with BsonValue

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

the class CommandMonitoringTest method getExpectedEvents.

private List<CommandEvent> getExpectedEvents(final BsonArray expectedEventDocuments) {
    List<CommandEvent> expectedEvents = new ArrayList<CommandEvent>(expectedEventDocuments.size());
    for (Iterator<BsonValue> iterator = expectedEventDocuments.iterator(); iterator.hasNext(); ) {
        BsonDocument curExpectedEventDocument = iterator.next().asDocument();
        String eventType = curExpectedEventDocument.keySet().iterator().next();
        BsonDocument eventDescriptionDocument = curExpectedEventDocument.getDocument(eventType);
        CommandEvent commandEvent;
        if (eventType.equals("command_started_event")) {
            commandEvent = new CommandStartedEvent(1, null, databaseName, eventDescriptionDocument.getString("command_name").getValue(), eventDescriptionDocument.getDocument("command"));
        } else if (eventType.equals("command_succeeded_event")) {
            BsonDocument replyDocument = eventDescriptionDocument.get("reply").asDocument();
            commandEvent = new CommandSucceededEvent(1, null, eventDescriptionDocument.getString("command_name").getValue(), replyDocument, 1);
        } else if (eventType.equals("command_failed_event")) {
            commandEvent = new CommandFailedEvent(1, null, eventDescriptionDocument.getString("command_name").getValue(), 1, null);
        } else {
            throw new UnsupportedOperationException("Unsupported command event type: " + eventType);
        }
        expectedEvents.add(commandEvent);
    }
    return expectedEvents;
}
Also used : CommandFailedEvent(com.mongodb.event.CommandFailedEvent) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) ArrayList(java.util.ArrayList) CommandEvent(com.mongodb.event.CommandEvent) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

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