Get last inserted document with tag MongoDB Java -
i working in java using 2.6 java driver.
i using "i" tag identify site.
what best way query in order last inserted document "i" = "99159" ?
please provide example code.
an example document in mongo:
{ "_id": objectid("560bc0eee4b01a37814ee444"), "datadate": "2015-09-30t11:00:00z", "i": "99159", "lat": "50.61359", "lon": "-1.95875", "name": "swanage", "country": "england", "continent": "europe", "elevation": "10.0", "period": { "type": "day", "value": "2015-09-30z", "rep": { "h": "75.9", "t": "14.7", "dp": "10.5", "minsincemidnight": "600" } } }
as have _id
objectid
, can sort data on _id
last inserted record.
sample code:
mongoclient mongoclient = new mongoclient(new serveraddress("localhost", 27017)); mongoclient mongo = new mongoclient("localhost", 27017); db mongodb = (db) mongo.getdb("testdb"); dbcollection collection = mongodb.getcollection("collection"); basicdbobject findobject = new basicdbobject().append("i","99159"); basicdbobject sortobject = new basicdbobject().append("_id", -1); dbcursor cur = collection.find(findobject).sort(sortobject).limit(1); dbobject obj = cur.one();
Comments
Post a Comment