(x, y)
如何在java中编写此查询?????在这里,学生是收集字段rollno,名称和标记,我必须根据他们的滚动数字找到学生的百分比。我无法编写用于添加标记的代码,因为添加运算符不支持添加多个值。
答案 0 :(得分:0)
这似乎有效。还有其他构建器模式/便利,但这暴露了所有工作。并为丰富的动态建筑留出空间。
DBCollection coll = db.getCollection("student");
List<DBObject> pipe = new ArrayList<DBObject>();
/**
* Clearly, lots of room for dynamic behavior here.
* Different sets of marks, etc. And the divisor is
* the length of these, etc.
*/
String[] marks = new String[]{"$marks1","$marks2","$marks3"};
DBObject add = new BasicDBObject("$add", marks);
List l2 = new ArrayList();
l2.add(add);
l2.add(marks.length); // 3
DBObject divide = new BasicDBObject("$divide", l2);
DBObject prjflds = new BasicDBObject();
prjflds.put("rollno", 1);
prjflds.put("per", divide);
DBObject project = new BasicDBObject();
project.put("$project", prjflds);
pipe.add(project);
AggregationOutput agg = coll.aggregate(pipe);
for (DBObject result : agg.results()) {
System.out.println(result);
}