Java MongoDB 3.0+ query between timestamps -
how query database entries within 2 timestamps in java mongodb 3.0?
currently using java method, doesnt in returning set of documents within start , end timestamp criteria. code bit broken because returns latest entry if falls within timestamp.
public static reading getreadingsbetween(string type,timestamp starttime,timestamp endtime) { mongoclient mongo = new mongoclient("localhost", 27017); mongodatabase db = mongo.getdatabase("sampledb"); mongocollection<document> newcoll; gson gson = new gson(); newcoll = db.getcollection("samplecollection"); document latestentry = newcoll.find().iterator().next(); string json = latestentry.tojson(); reading reading = gson.fromjson(json, reading.class); string thistimestamp = reading.getgw_timestamp(); dateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss"); date parsedtimestamp = null; try { parsedtimestamp = df.parse(thistimestamp); } catch (parseexception e) { return null; } timestamp gwtimestamp = new timestamp(parsedtimestamp.gettime()); mongo.close(); if (gwtimestamp.after(starttime) && gwtimestamp.before(endtime)) { return reading; } return null; }
what want set of database entries within 2 timestamps. how do in java mongodb 3.0?
the basic query (using 3.x driver functions)
finditerable<document> findcursor = newcoll.find( filters.and( filters.gte("timestamp_field", starttime), filters.lte("timestamp_field", endtime)));
this yield collection of document objects (and map them expected result.)
Comments
Post a Comment