如何使用聚合框架在 MongoDB java 中给出多个投影
How to give Multiple Projections in MongoDB java using Aggregation Framework
我有三个文件
{ "_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 }
我必须将考试总字段查询为期末考试+期中考试+额外分数的总和。
查询如下:
查询 1:
db.students.aggregate([ { "$project": { "final": 1, "midterm": 1, "examTotal": { "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 10 ] } ] } } } ])
但是除此之外,我只需要为 ID 大于等于 1 且小于等于 2 的人获取此信息
所以我修改了代码如下:
查询 2:
db.students.aggregate([ { $match: { $and: [ { _id: { $gte: 1, $lte: 2 } }]}}, { "$project": { "final": 1, "midterm": 1, "examTotal": { "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 10 ] } ] } } } ])
如何将此代码转换为查询 1 的 Java 代码 我编写了代码并且正在运行?如何在现有代码中添加匹配阶段如下:
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);
如何将 $match 运算符添加到上面显示的代码中?有什么建议吗?
$and
operator in the $match
管道并不是真正必要的,因为您可以通过指定逗号分隔的表达式列表来隐式执行 AND 操作。
聚合管道可以重构为:
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 实施:
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);
}
}
}
我有三个文件
{ "_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 }
我必须将考试总字段查询为期末考试+期中考试+额外分数的总和。 查询如下: 查询 1:
db.students.aggregate([ { "$project": { "final": 1, "midterm": 1, "examTotal": { "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 10 ] } ] } } } ])
但是除此之外,我只需要为 ID 大于等于 1 且小于等于 2 的人获取此信息
所以我修改了代码如下: 查询 2:
db.students.aggregate([ { $match: { $and: [ { _id: { $gte: 1, $lte: 2 } }]}}, { "$project": { "final": 1, "midterm": 1, "examTotal": { "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 10 ] } ] } } } ])
如何将此代码转换为查询 1 的 Java 代码 我编写了代码并且正在运行?如何在现有代码中添加匹配阶段如下:
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);
如何将 $match 运算符添加到上面显示的代码中?有什么建议吗?
$and
operator in the $match
管道并不是真正必要的,因为您可以通过指定逗号分隔的表达式列表来隐式执行 AND 操作。
聚合管道可以重构为:
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 实施:
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);
}
}
}