Skip to content

Instantly share code, notes, and snippets.

@blvp
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save blvp/9876701 to your computer and use it in GitHub Desktop.

Select an option

Save blvp/9876701 to your computer and use it in GitHub Desktop.
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