Search in sources :

Example 51 with MongoCollection

use of com.mongodb.client.MongoCollection in project calcite by apache.

the class MongoTable method find.

/**
 * Executes a "find" operation on the underlying collection.
 *
 * <p>For example,
 * <code>zipsTable.find("{state: 'OR'}", "{city: 1, zipcode: 1}")</code></p>
 *
 * @param mongoDb MongoDB connection
 * @param filterJson Filter JSON string, or null
 * @param projectJson Project JSON string, or null
 * @param fields List of fields to project; or null to return map
 * @return Enumerator of results
 */
private Enumerable<Object> find(MongoDatabase mongoDb, String filterJson, String projectJson, List<Map.Entry<String, Class>> fields) {
    final MongoCollection collection = mongoDb.getCollection(collectionName);
    final Bson filter = filterJson == null ? null : BsonDocument.parse(filterJson);
    final Bson project = projectJson == null ? null : BsonDocument.parse(projectJson);
    final Function1<Document, Object> getter = MongoEnumerator.getter(fields);
    return new AbstractEnumerable<Object>() {

        public Enumerator<Object> enumerator() {
            @SuppressWarnings("unchecked") final FindIterable<Document> cursor = collection.find(filter).projection(project);
            return new MongoEnumerator(cursor.iterator(), getter);
        }
    };
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson)

Example 52 with MongoCollection

use of com.mongodb.client.MongoCollection in project graylog2-server by Graylog2.

the class V20190127111728_MigrateWidgetFormatSettingsTest method testMigrationWithMultipleChartColorMapping.

@Test
@MongoDBFixtures("V20190127111728_MigrateWidgetFormatSettings_withMultipleColorMappings.json")
public void testMigrationWithMultipleChartColorMapping() {
    final BasicDBObject dbQuery1 = new BasicDBObject();
    dbQuery1.put("_id", new ObjectId("5e2ee372b22d7970576b2eb3"));
    final MongoCollection<Document> collection = mongoDB.mongoConnection().getMongoDatabase().getCollection("views");
    migration.upgrade();
    final FindIterable<Document> views = collection.find(dbQuery1);
    final Document view1 = views.first();
    @SuppressWarnings("unchecked") final List<Document> widgets1 = (List) view1.get("state", Document.class).get("2c67cc0f-c62e-47c1-8b70-e3198925e6bc", Document.class).get("widgets");
    assertThat(widgets1.size()).isEqualTo(2);
    Set<Document> aggregationWidgets = widgets1.stream().filter(w -> w.getString("type").equals("aggregation")).collect(Collectors.toSet());
    assertThat(aggregationWidgets.size()).isEqualTo(1);
    final Document aggregationWidget = aggregationWidgets.iterator().next();
    final Document config = aggregationWidget.get("config", Document.class);
    final Document formattingSettings = config.get("formatting_settings", Document.class);
    @SuppressWarnings("unchecked") final List<Document> chartColors = (List) formattingSettings.get("chart_colors", List.class);
    assertThat(chartColors.size()).isEqualTo(4);
    final Document chartColor1 = chartColors.get(0);
    assertThat(chartColor1.getString("field_name")).isEqualTo("count()");
    assertThat(chartColor1.getString("chart_color")).isEqualTo("#e91e63");
    final Document chartColor2 = chartColors.get(1);
    assertThat(chartColor2.getString("field_name")).isEqualTo("avg(fields)");
    assertThat(chartColor2.getString("chart_color")).isEqualTo("#e81e63");
    final Document chartColor3 = chartColors.get(2);
    assertThat(chartColor3.getString("field_name")).isEqualTo("mean(man)");
    assertThat(chartColor3.getString("chart_color")).isEqualTo("#e91f63");
    final Document chartColor4 = chartColors.get(3);
    assertThat(chartColor4.getString("field_name")).isEqualTo("total(win)");
    assertThat(chartColor4.getString("chart_color")).isEqualTo("#e91fff");
}
Also used : Document(org.bson.Document) MongoCollection(com.mongodb.client.MongoCollection) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BasicDBObject(com.mongodb.BasicDBObject) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Rule(org.junit.Rule) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) FindIterable(com.mongodb.client.FindIterable) MongoDBInstance(org.graylog.testing.mongodb.MongoDBInstance) ObjectId(org.bson.types.ObjectId) MockitoJUnit(org.mockito.junit.MockitoJUnit) MockitoRule(org.mockito.junit.MockitoRule) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Before(org.junit.Before) BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) List(java.util.List) Document(org.bson.Document) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 53 with MongoCollection

use of com.mongodb.client.MongoCollection in project graylog2-server by Graylog2.

the class V20190127111728_MigrateWidgetFormatSettingsTest method testMigrationWithoutChartColorMapping.

@Test
@MongoDBFixtures("V20190127111728_MigrateWidgetFormatSettings_without_color_mapping.json")
public void testMigrationWithoutChartColorMapping() {
    final BasicDBObject dbQuery1 = new BasicDBObject();
    dbQuery1.put("_id", new ObjectId("5e2ee372b22d7970576b2eb3"));
    final MongoCollection<Document> collection = mongoDB.mongoConnection().getMongoDatabase().getCollection("views");
    migration.upgrade();
    final FindIterable<Document> views = collection.find(dbQuery1);
    final Document view1 = views.first();
    @SuppressWarnings("unchecked") final List<Document> widgets1 = (List) view1.get("state", Document.class).get("2c67cc0f-c62e-47c1-8b70-e3198925e6bc", Document.class).get("widgets");
    assertThat(widgets1.size()).isEqualTo(2);
    Set<Document> aggregationWidgets = widgets1.stream().filter(w -> w.getString("type").equals("aggregation")).collect(Collectors.toSet());
    assertThat(aggregationWidgets.size()).isEqualTo(1);
    final Document aggregationWidget = aggregationWidgets.iterator().next();
    final Document config = aggregationWidget.get("config", Document.class);
    final Document formattingSettings = config.get("formatting_settings", Document.class);
    assertThat(formattingSettings.get("chart_colors")).isNull();
}
Also used : Document(org.bson.Document) MongoCollection(com.mongodb.client.MongoCollection) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BasicDBObject(com.mongodb.BasicDBObject) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Rule(org.junit.Rule) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) FindIterable(com.mongodb.client.FindIterable) MongoDBInstance(org.graylog.testing.mongodb.MongoDBInstance) ObjectId(org.bson.types.ObjectId) MockitoJUnit(org.mockito.junit.MockitoJUnit) MockitoRule(org.mockito.junit.MockitoRule) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Before(org.junit.Before) BasicDBObject(com.mongodb.BasicDBObject) ObjectId(org.bson.types.ObjectId) List(java.util.List) Document(org.bson.Document) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

MongoCollection (com.mongodb.client.MongoCollection)53 Document (org.bson.Document)39 MongoDatabase (com.mongodb.client.MongoDatabase)21 List (java.util.List)16 FindIterable (com.mongodb.client.FindIterable)14 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)11 BasicDBObject (com.mongodb.BasicDBObject)10 Set (java.util.Set)9 ObjectId (org.bson.types.ObjectId)9 MongoCursor (com.mongodb.client.MongoCursor)7 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Bson (org.bson.conversions.Bson)7 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 BsonDocument (org.bson.BsonDocument)6 Block (com.mongodb.Block)5 UpdateOptions (com.mongodb.client.model.UpdateOptions)5