Sign In Register

How can we help you today?

Start a new topic

Is it possible to generate MongoDB ObjectIDs in cloud code?

Hi, is there a way to generate MongoDB ObjectIDs in cloud code? For use in subdocuments/subobjects stored inside an array inside a top-level MongoDB document.


Something like this, from the NodeJS MongoDB driver:

var ObjectID = require('mongodb').ObjectID;

var generatedId = new ObjectID();


Can't find any cloud code method to do that, but I might have missed it.

Thanks



I'll let GS support answer for certainty, but I'm pretty sure no.


We just use a Redis key as an auto incrementing counter as suggested in their docs:


var uniqueId = Spark.getRedis().incr("uniqueId");

 


1 person likes this

We use this to generate object ids:

 

function objectId (now)
{
    var timestamp = ((now || (new Date()).getTime()) / 1000 | 0).toString(16);

    return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function() {
        return (Math.random() * 16 | 0).toString(16);
    }).toLowerCase();
};

 


1 person likes this

Thanks, that seems like a good workaround.

Login to post a comment