How to add two fields in mongoDB using java driver -


db.student.aggregate([{$project:{rollno:1,per:{$divide:[{$add:["marks1","$marks2","$marks3"]},3]}}}]) 

how write query in java????? here,student collection fields rollno,name , marks , have find percentage of students according roll numbers . not able write code adding marks add operator not support multiple value's addition.

this seems work. there other builder pattern/conveniences exposes workings. , leaves room rich dynamic construction.

        dbcollection coll = db.getcollection("student");          list<dbobject> pipe = new arraylist<dbobject>();          /**                                                                                    *  clearly, lots of room dynamic behavior here.                                   *  different sets of marks, etc.  , divisor                                  *  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);          (dbobject result : agg.results()) {             system.out.println(result);         } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -