Skip to content

Instantly share code, notes, and snippets.

@matrixd
Created May 1, 2017 10:54
Show Gist options
  • Select an option

  • Save matrixd/41945eedc94319c2d050a5ae0166bf7e to your computer and use it in GitHub Desktop.

Select an option

Save matrixd/41945eedc94319c2d050a5ae0166bf7e to your computer and use it in GitHub Desktop.
MongoClient cl = new MongoClient();
MongoDatabase db = cl.getDatabase(mDb);
MongoCollection coll = db.getCollection(mColl);
double avg = 0.0;
long docs = 0;
long vals = 0;
System.out.println("Starting to query the data...");
Bson fields = fields(include(mField), excludeId());
long start = System.nanoTime();
MongoCursor<Document> cursor = coll.find().projection(fields).iterator();
while (cursor.hasNext()) {
docs++;
final Object field = cursor.next().get(mField);
if (field.getClass() == ArrayList.class) {
final List<Double> list = (List<Double>)field;
for (Double d : list) {
vals++;
avg += d/271403.0;
}
} else {
vals++;
avg += ((Double) field)/271403.0;
}
}
long done = System.nanoTime();
long elapsed = done - start;
System.out.println("avg is " + String.valueOf(avg) + " got in " + String.valueOf(elapsed/1000/1000) + "ms");
System.out.println(String.valueOf(docs) + " docs have been parsed and " + String.valueOf(vals) + " values have been processed");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment