问题
I have a very open-ended API that allows the frontend to query the backend with more-or-less arbitrary mongo queries. I'm having problems with _ids being stored as ObjectIds but passed around as hex strings. If the frontend gives me some complicated mongo query, I don't want to have to implement logic to comb through all the crazy mongo query operators to replace string _ids with ObjectIds.
Beyond that, objects in the system have user-defined schemas, and so there are cases where the backend can't know whether a certain field being queried should be an ObjectId or not.
Does anyone know how I could solve this problem? The best idea I've come up with so far is to always store strings instead of ObjectIds for user-defined fields, and to figure out how to parse mongo queries to replace string _ids with ObjectIds for known fields. This certainly isn't ideal, so I'm hoping there's a more elegant solution here.
来源:https://stackoverflow.com/questions/27979012/best-ways-to-provide-a-very-open-ended-api-over-mongo-db-with-regards-to-objec