How to give Multiple Projections in MongoDB java using Aggregation Framework -


i have 3 documents

{ "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 } { "_id" : 2, "quizzes" : [ 9, 10 ], "labs" : [ 8, 8 ], "final" : 95, "midterm" : 80 } { "_id" : 1, "quizzes" : [ 10, 6, 7 ], "labs" : [ 5, 8 ], "final" : 80, "midterm" : 75, "extramarks" : 10 } 

i have query exam total fields sum of final +midterm+extramarks. query follows: query1:

  db.students.aggregate([ { "$project": { "final": 1, "midterm": 1, "examtotal": { "$add": [ "$final", "$midterm", { "$ifnull": [ "$extramarks", 10 ] } ] } } } ]) 

but along need people id greater equal 1 , less equal 2

so have modified code following: query2:

 db.students.aggregate([ { $match: { $and: [ { _id: { $gte: 1, $lte: 2 } }]}}, { "$project": { "final": 1, "midterm": 1, "examtotal": { "$add": [ "$final", "$midterm", { "$ifnull": [ "$extramarks", 10 ] } ] } } } ]) 

how convert code java code query 1 made code , working? how add match stage in existing code follows:

bsonarray fields=new bsonarray();             bsonarray defaultvalue1=new bsonarray();             defaultvalue1.add(new bsonstring("$extramarks"));             defaultvalue1.add(new bsondouble(0d));                       bsondocument ifnullprojection=new bsondocument();             ifnullprojection.put("$ifnull",defaultvalue1);             fields.add(new bsonstring("$final"));             fields.add("$midterm");             fields.add(ifnullprojection);             bsondocument addobject=new bsondocument();             addobject.append("$add", fields);             bsondocument valuetobeupdate=new bsondocument();             valuetobeupdate.append("sum", addobject);             bsondocument mainprojection=new bsondocument();             mainprojection.append("$project", valuetobeupdate);             list<bsondocument> pipeline=new arraylist<bsondocument>();             pipeline.add(mainprojection);             aggregateiterable<document> iterable = refcollection.aggregate(pipeline); 

how add $match operator code shown above?any suggestions?

the $and operator in $match pipeline not necessary since can implicitly , operation specifying comma separated list of expressions.

the aggregation pipeline can restructured :

mongo shell:


/*     mongo shell:      var pipeline = [          {              "$match": { "_id": { "$gte": 1, "$lte": 2 } } // or  "$match": { "_id": { "$in": [1,2] } }         },          {              "$project": {                  "final": 1,                  "midterm": 1,                  "examtotal": {                      "$add": [ "$final", "$midterm", { "$ifnull": [ "$extramarks", 10 ] } ]                  }              }          }      ];     db.students.aggregate(pipeline);  */ 

java implementation:

public class javaaggregation {     public static void main(string args[]) throws unknownhostexception {          mongoclient mongo = new mongoclient();         db db = mongo.getdb("test");          dbcollection coll = db.getcollection("students");          // create pipeline operations, first $match         dbobject match = new basicdbobject("$match",                             new basicdbobject("_id",                                 new basicdbobject("$gte", 1).append("$lt", 2)                             )                         );          // build $project operations         basicdblist coalesce = new basicdblist();         coalesce.add("$extramarks");         coalesce.add(10)         dbobject ifnullclause = new basicdbobject("$ifnull", coalesce);            basicdblist addition = new basicdblist();         addition.add("$final");         addition.add("$midterm");         addition.add(ifnullclause);          dbobject examtotal = new basicdbobject("$add", addition);          dbobject fields = new basicdbobject("final", 1);         fields.put("midterm", 1);         fields.put("examtotal", examtotal);          dbobject project = new basicdbobject("$project", fields);         list<dbobject> pipeline = arrays.aslist(match, project);          aggregationoutput output = coll.aggregate(pipeline);          (dbobject result : output.results()) {             system.out.println(result);         }     } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -