1 package dst.ass1.doc.impl;
3 import com.mongodb.client.MapReduceIterable;
4 import com.mongodb.client.MongoCollection;
5 import com.mongodb.client.MongoDatabase;
6 import dst.ass1.doc.IDocumentQuery;
7 import dst.ass1.jpa.util.Constants;
8 import org.bson.Document;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.regex.Pattern;
14 import static com.mongodb.client.model.Filters.eq;
15 import static com.mongodb.client.model.Filters.regex;
17 public class DocumentQuery implements IDocumentQuery {
18 private MongoDatabase db;
20 public DocumentQuery(MongoDatabase db) {
25 public Document findByMaterialId(Long materialId) {
26 MongoCollection<Document> coll = db.getCollection(Constants.COLL_MATERIAL_DATA);
27 return coll.find(eq(Constants.I_MATERIAL, materialId)).first();
31 public List<Long> findIdsByType(String typeSubstring) {
32 MongoCollection<Document> coll = db.getCollection(Constants.COLL_MATERIAL_DATA);
33 List<Long> ret = new ArrayList<>();
34 for (Document doc : coll.find(regex(Constants.M_MATERIAL_TYPE, ".*" + Pattern.quote(typeSubstring) + ".*"))) {
35 ret.add(Long.valueOf("" + doc.get(Constants.I_MATERIAL)));
42 public List<Document> getDocumentStatistics() {
43 MongoCollection coll = db.getCollection(Constants.COLL_MATERIAL_DATA);
45 String map = "function(){if(this.type=='quiz')emit(this.quiz_type,this.questions.length);}";
46 String reduce = "function(key, values) { " +
48 "values.forEach(function(value){result += value;});" +
51 MapReduceIterable<Document> iter = coll.mapReduce(map, reduce);
52 ArrayList<Document> a = iter.into(new ArrayList<Document>());