Here are the sample data in my sales collection, how to retrieve the exact date as stored in date field in the sample sales collection :
/* 1 */
{
"_id" : ObjectId("5a4e591efce077c4d7e6f523"),
"date" : ISODate("2017-01-01T00:00:00.000Z"),
"name" : "testing 123"
}
/* 2 */
{
"_id" : ObjectId("5a4e591efce077c4d7e6f524"),
"date" : ISODate("2017-01-01T00:00:01.000Z"),
"name" : "Testing 231"
}
/* 3 */
{
"_id" : ObjectId("5a4e591efce077c4d7e6f525"),
"date" : ISODate("2017-01-01T23:59:59.000Z"),
"name" : "teSting 456"
}
Here is my java code :
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.CommandResult;
import org.bson.types.BasicBSONList;
public class MongoTest {
public static void main(String[] s) throws Exception {
Mongo mongo = new MongoClient("localhost");
DB db = new DB(mongo, "onixtest");
BasicDBObject obj = new BasicDBObject("find", "sales");
CommandResult commandResult = db.command(obj);
BasicDBObject o = (BasicDBObject) commandResult.get("cursor");
BasicDBList o1 = (BasicDBList) o.get("firstBatch");
System.out.println("------------------------------------");
//System.out.println(o1);
System.out.println("------------------------------------");
for(Object o11 : o1)
System.out.println(((BasicDBObject)o11).get("date"));
mongo.close();
}}
Here is the output of running the above java program:
Sun Jan 01 05:30:00 IST 2017
Sun Jan 01 05:30:01 IST 2017
Mon Jan 02 05:29:59 IST 2017
Regards kris