Discipol
Discipol

Reputation: 3157

How do I remove the need to convert _id (as a string) to new ObjectID(string) in nodejs / mongodb native library?

Each time I have to perform a query involving _id, I have to do new ObjectID( _idAsString) in order for it to work. I realize mongo tests the object, not the value itself, but this is adding a lot of overhead and I may miss converting it in some places.

The _id goes to the client where the ObjectID( string ) is turned to string and when it comes back from the client, I would have to remake it into ObjectID( string ). I will mention that "string" is the actual value generated by mongo, something like 123a1b12dc...

If there is another good/complete library with such a internal functionality, I would love to try it out.

Upvotes: 0

Views: 180

Answers (1)

Timothy Strimple
Timothy Strimple

Reputation: 23070

There are some Node.js object mappers which provide this functionality. Take a look at Mongolia.

https://github.com/masylum/mongolia#mappings-and-type-casting

Upvotes: 1

Related Questions