I have updated my meteor yesterday and tried using the new Meteor.Collection.ObjectID
.
But since with no success. First i updated my collections in this way:
myCollection = new Meteor.Collection('mycollection', {idGeneration: 'MONGO'}
Now, normal new inserts have an _id
like Wi2RmR6CSapkmmdfn
... (?)
Then i have a collection with an array included. I like to have an unique id for every object in this array. So i $push
an object with a field like id: new Meteor.Collection.ObjectID()
into my array. The result in the database is like this: ObjectId("5b5fc278305d406cc6c33756")
. (This seems to be normal.)
But later i want to update my pushed object, if the id
equals an id, which i stored as data attribute in an html tag before.
var equals = EJSON.equals(dbId, htmlId);
(This results every time in false
. So i logged the values dbId
and htmlId
into the console with console.log(typeof dbId, dbId);
)
The values of this two variables is as follows:
object { _str: 'a86ce44f9a46b99bca1be7a9' }
(dbId)
string ObjectID("a86ce44f9a46b99bca1be7a9")
(htmlId; this seems to be correct, but why is a custom type a string?)
How to use the Meteor.Collection.ObjectID
correct?
When placing your htmlId
in your html you need to put it in as a string and not as an object, remember _id
is an object now, handlebars is guessing and using toString()
& thats why it shows up as ObjectID("...")
.
So if you're using {{_id}}
in your html you now need to use {{_id.toHexString}}
to properly extract the string part of it out
When you extract this html value with your javascript you need to make it back into an objectid:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With