Implement Map-Reduce (1.4.3)

This commit is contained in:
Tobias Eidelpes 2021-04-07 11:51:55 +02:00
parent d5df23a8c3
commit 135c7dfd28

View File

@ -56,6 +56,33 @@ public class DocumentQuery implements IDocumentQuery {
@Override @Override
public List<Document> getDocumentStatistics() { public List<Document> getDocumentStatistics() {
return null; MongoCollection<Document> collection = db.getCollection(COLL_LOCATION_DATA);
String map =
"function() {" +
"if (this.type === 'place') {" +
"emit(this.category, 1);" +
"}" +
"}";
String reduce =
"function(key, values) {" +
"sum = 0;" +
"values.forEach(value => {sum += value});" +
"return sum;" +
"}";
List<Document> result = new ArrayList<>();
MongoCursor<Document> cursor = collection.mapReduce(map, reduce).iterator();
try {
while (cursor.hasNext()) {
result.add(cursor.next());
}
} finally {
cursor.close();
}
return result;
} }
} }