Handling of ObjectId properties
henit opened this issue · 4 comments
Hi. I think Mongorito looks very nice, and consider using it. Two questions:
-
In normal direct queries to a database (using the mongodb-driver), properties saved as ObjectId has to be cast to ObjectId. Like if I run
category.find({ parentCategory: 'abcdef' })
it does not find any documents, I have to convert the id-string before passing it as a query condition. Is this the same with Mongorito, or is there a way to avoid this? -
And, is it with Mongorito possible to get all ObjectId properties on documents returned by find operations etc converted to strings so I don't have to do this manually? And the same for converting back before insert/update operations.
-
Unfortunately, yes, you need to cast your value to
ObjectId
if you want to get the correct results. Previous iteration of Mongorito had automatic casting toObjectId
, but people were opening issues about it not being compatible with their custom ids (e.g. in Meteor). -
I imagine this could be possible using a custom middleware, but it would be a risky one, since it might affect Mongorito's internals.
Thanks. In them name of functional programming, I could just wrap the props in a reusable convertion function before passing it to the db-functions. Should work in most cases so I don't have to repeat the casting back and forward all the time, since most of the project code works by having reference properties as strings.
@vadimdemedes Btw, about the compatibility issue you mentioned.. Could that have been solved with an optional feature? Like enabling an option that triggers automatic casting, so people that need to cast manually can just skip enabling autocast, and the rest of us can avoid repeated casting syntaces all over our code...
I think having it as an external plugin would be better. I'll add it to my todo list.