Meteor 0.5.7: how to handle / use Meteor.Collection.ObjectID?

Yesterday I updated my meteorite and tried to use a new one Meteor.Collection.ObjectID. But so without success. First, I updated my collections in this way:

myCollection = new Meteor.Collection('mycollection', {idGeneration: 'MONGO'} Now normal new inserts have _idlike Wi2RmR6CSapkmmdfn... (?)

Then I have a collection with the array turned on. I like to have a unique identifier for each object in this array. So, I am $pushan object with a field similar id: new Meteor.Collection.ObjectID()to my array. Result in the database as follows: ObjectId("5b5fc278305d406cc6c33756"). (This seems normal.)

But later, I want to update my pushed object if it idis equal to the id that I previously saved as a data attribute in the html tag.

var equals = EJSON.equals(dbId, htmlId);(This is the result each time false. So I registered values dbIdand htmlIdthe console via console.log(typeof dbId, dbId);)

The values ​​of these two variables are as follows:

object { _str: 'a86ce44f9a46b99bca1be7a9' } (dbId)

string ObjectID("a86ce44f9a46b99bca1be7a9") (htmlId; this seems correct, but why a custom string type?)

How to use Meteor.Collection.ObjectID?

+5
source share
1 answer

When placed htmlIdin html, you need to place it as a string, and not as an object, remember that _idthis is an object now, galdies are guessing and using toString(), and that is why it appears as ObjectID("...").

So, if you use {{_id}}in your html, now you need to use {{_id.toHexString}}part of the string to correctly extract it

html javascript, :

JS:

var valuefromhtml = "a86ce44f9a46b99bca1be7a9"; //Get with Jquery,DOM,etc

htmlId = new Meteor.Collection.ObjectID(valuefromhtml); //see: http://docs.meteor.com/#collection_object_id

EJSON.equals(htmlId, dbId); //Should be true this time    
+8

All Articles