Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to remove ObjectID() from _id using Meteor

Tags:

mongodb

meteor

I am retrieving records from Mongo using Meteor. I use the {{_id}} placeholder in my meteor template to use the _id field of the record, but it adds this into my template....

ObjectID("54f27a1adfe0c11c824e04e9")

... and I would like just to have...

54f27a1adfe0c11c824e04e9

How do I do this?

like image 941
JoeTidee Avatar asked Mar 28 '15 23:03

JoeTidee


People also ask

Can we override _id in MongoDB?

The _id field is immutable—that is, once a document exists in your MongoDB system, it has, by definition, been assigned an _id, and you cannot change or update its primary key. That said, _id can be overridden when you insert new documents, but by default it will be populated with an ObjectID.

What is ObjectID in BSON?

An ObjectID is a 12-byte Field Of BSON type. The first 4 bytes representing the Unix Timestamp of the document. The next 3 bytes are the machine Id on which the MongoDB server is running. The next 2 bytes are of process id. The last Field is 3 bytes used for increment the objectid.

Why MongoDB ID is ObjectID?

MongoDB uses ObjectIds as the default value of _id field of each document, which is generated during the creation of any document. Object ID is treated as the primary key within any MongoDB collection. It is a unique identifier for each document or record.


2 Answers

Just add a global helper:

Template.registerHelper('formatId', function(data) {
  return (data && data._str) || data;
});

You can also do it like this with ES6 syntax :

Template.registerHelper('formatId', (id) => (id && id._str) || id);

And use it in any template like this:

{{formatId _id}}

This will work for both mongo-style ObjectIds and meteor-style random strings.

like image 153
David Weldon Avatar answered Nov 15 '22 08:11

David Weldon


Since your documents are using the MongoDB ID format rather than the default Meteor ID format (simply a randomly generated string), you will need to use the MongoDB ObjectId.toString() function described here. But unfortunately, this simply results in your ObjectID being printed out as a string like ObjectID("54f27a1adfe0c11c824e04e9").

I would recommend creating a document ID template helper that cleans your document IDs before including them in the template. Since this issue is most likely related to all of your documents in all of your collections, I would further suggest creating a global template helper. This can be done like so:

Template.registerHelper('cleanDocumentID', function(objectID) {
    var objectIdString = objectID.toString();
    var cleanedString = objectIDString.slice(objectIDString.indexOf('"') + 1, -2);

    return cleanedString;
});

This template helper slices out just the actual object ID string from the string provided by the ObjectId.toString() function. You can use this template helper in your templates like so:

{{cleanDocumentID _id}}

I know that this is a lot messier than simply using the document ID in the template like {{_id}}, but it is necessary due to the fact that your documents have the MongoDB ObjectID-type document ID rather than the simple randomly generated string as used by Meteor by default.

If you would like to learn more about how to set your MongoDB collections to use randomly generated strings for document IDs and avoid this mess, check out this.

like image 26
Keith Dawson Avatar answered Nov 15 '22 06:11

Keith Dawson