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);
}
};
}
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");
}
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();
}
Aggregations