Skip to content

Instantly share code, notes, and snippets.

@kommradHomer
Last active July 31, 2020 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kommradHomer/e38007f8f03fcd2f792c8acbf7b75da3 to your computer and use it in GitHub Desktop.
Save kommradHomer/e38007f8f03fcd2f792c8acbf7b75da3 to your computer and use it in GitHub Desktop.
bucket used
Bson a=Aggregates.match( new Document("year", 5));
BucketOptions o=new BucketOptions();
o.output(
new BsonField("student_ids", new Document("$push","$student_id")),
new BsonField("attended_class",new Document("$sum",
new Document("$cond",
Arrays.asList(
new Document("$gte",Arrays.asList("$attn",8)),
1,
0
)
)
)
),
new BsonField("students_total", new Document("$sum",1))
);
Bson b=Aggregates.bucket("$final_grade", Arrays.asList(0,45,55,70,85,100),o);
//LIST for the pipeline
List<Bson> l=new ArrayList<>();
l.add(a);
l.add(b);
//STUDENTS_COLLECTION mongodb collection assumed to be provided
MongoCursor cur=STUDENTS_COLLECTION.aggregate(l).iterator();
//print as JSON
while(cur.hasNext())
System.out.println(((Document) cur.next()).toJson());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment