ThomasReggi
ThomasReggi

Reputation: 59485

Inserting JSON into MongoDB converting dates from strings automatically

I have objects that use the keys updated_at and created_at which have string timestamps like 2012-08-29T16:04:34-04:00. I'm inserting this into MongoDB. The catch is each object can have a variable number of instances of updated_at and created_at (they are in arrays within). Is there any code out there that can be used to search an array for updated_at and created_at and replace the values with $.created_at = new Date($.created_at)?

{
    "name":"thomas",
    "created_at":"2012-08-29T16:04:34-04:00",
    "updated_at":"2012-08-29T16:04:34-04:00",
    "logs":[
        {
            "something":"something",
            "created_at":"2012-08-29T16:04:34-04:00",
        },
        {
            "something":"something",
            "created_at":"2012-08-29T16:04:34-04:00",
        },
    ]
}

to

{
    "name":"thomas",
    "created_at":new Date("2012-08-29T16:04:34-04:00"),
    "updated_at":new Date("2012-08-29T16:04:34-04:00"),
    "logs":[
        {
            "something":"something",
            "created_at":new Date("2012-08-29T16:04:34-04:00"),
        },
        {
            "something":"something",
            "created_at":new Date("2012-08-29T16:04:34-04:00"),
        },
    ]
}

Upvotes: 1

Views: 2484

Answers (2)

Billy Moon
Billy Moon

Reputation: 58601

// store your data object in x
x = {
    "name":"thomas",
    "created_at":"2012-08-29T16:04:34-04:00",
    "updated_at":"2012-08-29T16:04:34-04:00",
    "logs":[
        {
            "something":"something",
            "created_at":"2012-08-29T16:04:34-04:00",
        },
        {
            "something":"something",
            "created_at":"2012-08-29T16:04:34-04:00",
        },
    ]
}

// create a traversal function to recurse
function traverse(o) {
    // loop through object
    for (i in o) {
        // if it is a matched key (current regex matches created_at or updated_at)
        // parse the item as a date, and re-store object
        if(i.match(/(cre|upd)ated_at/)){
            o[i] = new Date(o[i])
        }
        // if the key we are looking at is an object, then recurse!
        if (typeof(o[i])=="object") {
            traverse(o[i])
        }
    }
}

// fire it up!
traverse(x)

// check the results
console.dir(x)

Upvotes: 1

Billy Moon
Billy Moon

Reputation: 58601

// store your data object in x
x = {
    "name":"thomas",
    "created_at":"2012-08-29T16:04:34-04:00",
    "updated_at":"2012-08-29T16:04:34-04:00",
    "logs":[
        {
            "something":"something",
            "created_at":"2012-08-29T16:04:34-04:00",
        },
        {
            "something":"something",
            "created_at":"2012-08-29T16:04:34-04:00",
        },
    ]
}

// loop through each element of the logs array
for(y in x.logs){
    // modify the `created_at` value of the y-th element
    // by wrapping with the desired string
    x.logs[y].created_at = "new Date(" + x.logs[y].created_at + ")"
}

// check the final format of the object
console.dir(x)

Caveat:

The object stores a string containing new Date ... ammendment - to store the result of the operation, you would need to adjust the modification line to...

x.logs[y].created_at = new Date( x.logs[y].created_at )

Upvotes: 0

Related Questions