0

I've a mongo db collection as below.

{
    "_id": {
        "$oid": "57048f2f60b18f8e186e65b6"
    },
    "ID": 1,
    "Room_ID": 303,
    "StartTime": "2016-03-12T11:00:00Z",
    "EndTime": "2016-03-12T12:00:00Z",
    "Login_ID": "ABCDE"
}

I'm using db.collection.find() query to fetch the results. I am trying to display the date in two parts 1. DD-MM-YY and 2. HH-MM-SS. I've two ideas for this. 1. Get the date in the desired format from collection.find() command or 2. After fetching the records into cursor, parse it in angularjs code to separate date and time values.

I've tried the $dateToString of mongodb but couldn't get the results. In fact I am confused with it.

db.RoomReservation.aggregate({ $project: {"Login_ID": "ABCDE", dt: { $dateToString: { format: "%Y-%m-%d", date: "$StartTime"}}}});
assert: command failed: {
        "errmsg" : "exception: can't convert from BSON type String to Date",
        "code" : 16006,
        "ok" : 0
} : aggregate failed

Can someone please help me in getting the output of collection.find() in the following format

Room_ID, Date, StartTime, EndTime, Login_ID
303, 2016-03-12, 11:00, 12:00, ABCDE
3
  • This is for MongoDB versions 3.0 or higher. What version of MongoDB are you using? Commented Apr 21, 2016 at 3:50
  • Hi, I'm using 3.2.4 version Commented Apr 21, 2016 at 3:54
  • you should leave date formatting to UI layer. There are capable libraries like Moment.js to display the date in any desired formats. Commented Apr 21, 2016 at 10:10

1 Answer 1

3

You can simply parse your date in angular part using filters , if you want ..

     <span>{{ | date:'dd-MM-yyyy @ HH:mm:ss Z'}}</span>

you can refer for more date options here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the easy work around. It helped me, though I had to figure out solution the timezone issue. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.