title: How to give Multiple Projections in MongoDB java using Aggregation Framework
body: <p>i have three documents </p>
  
  <pre><code>{ "_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 }
  </code></pre>
  
  <p>I have to query exam total fields as sum of final +midterm+extramarks.
  The query is as follows:
  <strong>Query1:</strong></p>
  
  <pre><code>  db.students.aggregate([ { "$project": { "final": 1, "midterm": 1, "examTotal": { "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 10 ] } ] } } } ])
  </code></pre>
  
  <p>But along with this i need to get this only for people whose id is greater than equal to 1 and less than equal to 2</p>
  
  <p>So i have modified the code to the following:
  <strong>Query2:</strong></p>
  
  <pre><code> db.students.aggregate([ { $match: { $and: [ { _id: { $gte: 1, $lte: 2 } }]}}, { "$project": { "final": 1, "midterm": 1, "examTotal": { "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 10 ] } ] } } } ])
  </code></pre>
  
  <p>How to convert this code to Java code for query 1 i made the code and is working? How to add match stage in the existing code which is as follows:</p>
  
  <pre><code>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&lt;BsonDocument&gt; pipeline=new ArrayList&lt;BsonDocument&gt;();
              pipeline.add(mainProjection);
              AggregateIterable&lt;Document&gt; iterable = refCollection.aggregate(pipeline);
  </code></pre>
  
  <p><strong>How to add $match operator to the code shown above?Any suggestions?</strong></p>