Last active
August 29, 2015 13:57
-
-
Save blvp/9876701 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.mongodb.*; | |
| import java.net.UnknownHostException; | |
| import java.util.Optional; | |
| import java.util.stream.Stream; | |
| public class SomeApp { | |
| public static void main(String[] args) throws UnknownHostException { | |
| MongoClient client = new MongoClient(); | |
| DB db = client.getDB("school"); | |
| DBCollection students = db.getCollection("students"); | |
| assert(students.count()==200); | |
| System.out.println(students.findOne()); | |
| students.find() | |
| .forEach(student -> { | |
| Optional<Object> min = ((BasicDBList) student.get("scores")) | |
| .stream() | |
| .filter(filterScoreType) | |
| .min(scoreComparator); | |
| students.update(student, new BasicDBObject("$pull", new BasicDBObject("scores", min.get()))); | |
| }); | |
| System.out.println(students.findOne()); | |
| } | |
| private static Predicate<Object> filterScoreType = t -> ((BasicDBObject) t).getString("type").equals("homework"); | |
| private static Comparator<Object> scoreComparator = | |
| (o1, o2) -> Double.compare(((BasicDBObject) o1).getDouble("score"), ((BasicDBObject) o2).getDouble("score")); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment