Reputation: 1017
i have three 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 to query exam total fields as sum of final +midterm+extramarks. The query is as follows: Query1:
db.students.aggregate([ { "$project": { "final": 1, "midterm": 1, "examTotal": { "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 10 ] } ] } } } ])
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
So i have modified the code to the 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 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:
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 to add $match operator to the code shown above?Any suggestions?
Upvotes: 0
Views: 4710
Reputation: 103435
The $and
operator in the $match
pipeline is not really necessary since you can implicitly do an AND operation by just specifying a comma separated list of expressions.
The aggregation pipeline can be restructured to :
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 the pipeline operations, first with the $match
DBObject match = new BasicDBObject("$match",
new BasicDBObject("_id",
new BasicDBObject("$gte", 1).append("$lt", 2)
)
);
// build the $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);
for (DBObject result : output.results()) {
System.out.println(result);
}
}
}
Upvotes: 2