Start a new topic

Creating ObjectIDs

Sorry if this is a duplicate post. I posted and can't find it anywhere.



How can I generate Mongo objectIDs myself.



In Node.js, I'd do the following :



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

var myObjectID = new ObjectID();



Kinvey provides the collectionAccess.objectId() method. Unfortunately, this only CONVERTS existing strings to objectIDs. Can y'all rewrite this method so that it generates objectIds if no string is provided?




What's the use case? Are you looking to generate ObjectIDs for attributes other than _id?
I will have a collection with documents that have subdocuments. Those subdocuments need a unique id that will be tied in a many-to-many relationship in another collection. I can create a GUID, but that is overkill size wise. The MongoDB ObjectID would work perfectly for this.
Actually the objectID method will also generate a random objectID:



function onRequest(request, response, modules){

var id = modules.collectionAccess.objectID();

response.body = {id: id};

response.complete();

}



This function will return a generic objectID.
Login or Signup to post a comment