This is a follow-up to the question here Using map/reduce for mapping the properties in a collection.
I currently have a collection containing documents where each document is a form submission. There are multiple forms submissions held within a single mongoDB collection and can be differentiated from one another by the value held within a (key, value) pair with key=formID
.
I currently have a very lazy method (below) for finding all the keys for documents with a specific formID
, however hoped that it would be possible to optimize this by using MongoDBs MapReduce functionality.
Bson bsonFilter = Filters.eq("formid", formId);
FindIterable<Document> submissionsById = collection.find(bsonFilter);
Set<String> keys = new HashSet<>();
for (Document submission : submissionsById){
keys.addAll(submission.keySet());
}
JSONArray keysArray = new JSONArray();
keys.forEach(keysArray::put);
Is this possible, or is the rudimentary solution I've come up with going to be as good as I can manage? Thanks.