teadrinker
teadrinker

Reputation: 45

how to replace ObjectId in JavaScript with Epoch Time

How do I iterate through a JSON string and replace every ObjectId into Unix Epoch time for further processing?

What I know: You get the first 8 chars from an objectId with:

subStrObjectId = objectId.substring(0, 8);

5668d142a54cc4960b55ea19 --> 5668D142

and convert these from hexadecimal into an Int value (epoch time in milliseconds):

subStrObjectIdInDec = parseInt(subStrObjectId, 16);

5668D142 (hex) --> 1449709890 (dec)

my Json string:

myJsonString = [
    [
        {"_id":"5668d142a54cc4960b55ea19","cid":10851045,"temp":25.4},
        {"_id":"5668d14ea54cc4960b55ea1a","cid":10850909,"temp":24.9}
    ],
    [
        {"_id":"5668d14fa54cc4960b55ea1b","cid":10851045,"hum":37.9},
        {"_id":"5668d3108c8cda92074b7ec9","cid":10850909,"hum":39.6}
    ],
    [
        {"_id":"5668d3198c8cda92074b7ecb","cid":10851045,"lux":34},
        {"_id":"5668d31e8c8cda92074b7ecc","cid":10850909,"lux":68}
    ]
];

Upvotes: 2

Views: 329

Answers (2)

Sede
Sede

Reputation: 61225

You can use the getTimestamp() to return the timestamp portion of the ObjectId() as a Date.

myJsonString.map(
    function(element) {
        return element.map(function(doc) {
            doc.timestamp = ObjectId(doc._id).getTimestamp(); 
            doc._id = ObjectId(doc._id); // Only if you want to convert your _id string to valid Objectid
            return doc;
        })
    }
);

Which yields:

[       
    [
            {
                    "_id" : ObjectId("5668d14fa54cc4960b55ea1b"),
                    "cid" : 10851045,
                    "hum" : 37.9,
                    "timestamp" : ISODate("2015-12-10T01:11:43Z")
            },
            {
                    "_id" : ObjectId("5668d3108c8cda92074b7ec9"),
                    "cid" : 10850909,
                    "hum" : 39.6,
                    "timestamp" : ISODate("2015-12-10T01:19:12Z")
            }
    ],
    [
            {
                    "_id" : ObjectId("5668d3198c8cda92074b7ecb"),
                    "cid" : 10851045,
                    "lux" : 34,
                    "timestamp" : ISODate("2015-12-10T01:19:21Z")
            },
            {
                    "_id" : ObjectId("5668d31e8c8cda92074b7ecc"),
                    "cid" : 10850909,
                    "lux" : 68,
                    "timestamp" : ISODate("2015-12-10T01:19:26Z")
            }
    ]
]

Upvotes: 1

Aᴍɪʀ
Aᴍɪʀ

Reputation: 7803

If you write what you know as a function:

function convert(id) {
  return parseInt(id.substring(0, 8), 16);
}

You can easily iterate over your objects and run your function on the objects.

I'd prefer functional-style javascript:

var data = myJsonString.map(function (array) {
  return array.map(function (item) {
    item.time = convert(item._id);
    return item;
  })
})

console.log(data);

But you can go iterative with loops as well:

for (var i=0;i<myJsonString.length;i++) {
  for (var j=0;j<myJsonString[i].length;j++) {
    myJsonString[i][j].time = convert(myJsonString[i][j]._id);
  }
}
console.log(myJsonString);

Upvotes: 3

Related Questions