MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Query to fetch data in right time format below

I am using below query
var cursor =db.getCollection(‘user_info’).find({})
while (cursor.hasNext()) {
var record = cursor.next();
print(record.Login +’|’+ record.Email +’|’ + record.LastLoginDate )
}

Query is pulling back the right fields with no problem, but not in the right time format – even though when looking at the field in its normal state, it is in the right format.
Example:
Sun Dec 15 2019 18:50:37 GMT-0800 (PST) <-- query returns this
ISODate(“2020-04-01T19:29:27.522Z”) **<-- field contains this
** Any ideas about how to adjust the query to return the date fields in this format?
YYYY-MM-dd’T’HH:mm:ss.SSS

The query returns the date type only, it is the print method which prints the date as the string version of the date. Use the printjson instead of print.

See: Date data type in shell

Hi Prasad…
I tried with printjson as well.It is giving same result as earlier…

Oops…sorry it worked…Thanks a lot…

when i have added printjson(record.LastLoginDate) it is showing correct format,
But when i am using printjson(record.Login +’|’+ record.Email +’|’ + record.LastLoginDate )again the result is showing in string format…how to solve this issue?

Okay, try this:

var cursor = db.getCollection('user_info').find();

while (cursor.hasNext()) {

    var record = cursor.next();
    var printStr = record.Login + '|' + record.Email + '|' + JSON.stringify(record.LastLoginDate);
    print(printStr);
}

Thanks it worked… As Date is in ISO date(IST)format can you tell me if there is any query or way to convert into PST from…