以下是我的销售合约中的示例数据,如何检索样本销售合约中日期字段中存储的确切日期:
/* 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"
}
这是我的java代码:
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();
}}
以下是运行上述java程序的输出:
Sun Jan 01 05:30:00 IST 2017
Sun Jan 01 05:30:01 IST 2017
Mon Jan 02 05:29:59 IST 2017
此致 克里斯