0

I am using mongodb3.0.5 and my collection looks like this:

{
    "_id" : "xxxxxxxxxxxxxxx",
    "SchoolId" : 1,
    "ActivationTimestamp" : ISODate("2015-09-22T13:01:58.000Z"),
    "PersonDetails" : [ 
        {
            "Name" : "John",
            "AddressZone" : 6,
        }, 
        {
            "Name" : "Mary",
            "AddressZone" : 5,
        },
    ],
    "CreationTimestamp" : ISODate("2015-11-10T10:55:00.009Z")
}

I have a js file like this:

var printData = function(doc){
        print(doc.CreationTimestamp+","+doc.SchoolId+","+doc.PersonDetails.Name+","+doc.PersonDetails.AddressZone)
    };

var cur = 
    db.test.aggregate([
        {$match: {_id: "xxxxxxxxxxxxxxx"}},
        {$unwind: '$PersonDetails'}]);

cur.forEach(printData);

If I run this command:

.\mongo localhost/test test.js > output.txt

I get the following ouput(didn't include all lines just a sample):

Tue Sep 22 2015 14:01:58 GMT+0100 (GMT Daylight Time),1,John,6

But I would like the date to have a format of something like:

22/09/2015 14:01:58

Is there a function or something to output the datetime as above?

1 Answer 1

4

You can use the date and put it in a javascript date object:

var d = new Date(doc.CreationTimestamp);

Now you can massage it however you want:

var date = d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();

Check http://www.w3schools.com/jsref/jsref_obj_date.asp for all the functions you can call on a date object.

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

Comments

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.