Best way(s) to provide a very open-ended api over mongo db with regards to ObjectIds

蹲街弑〆低调 提交于 2019-12-12 04:57:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!