Facebook user_id as MongoDB BSON ObjectId?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 17:33:37

问题


I'm rebuilding Lovers on Facebook with Sinatra & Redis. I like Redis because it doesn't have the long (12-byte) BSON ObjectIds and I am storing sets of Facebook user_ids for each user. The sets are requests_sent, requests_received, & relationships, and they all contain Facebook user ids.

I'm thinking of switching to MongoDB because I want to use it's geospatial indexing. If I do, I'd want to use the FB user ids as the _id field because I want the sets to be small and I want the JSON responses to be small. But, is the BSON ObjectId better (more efficient for MongoDB) to use than just an integer (fb user_id)?


回答1:


There are no major efficiency differences as far as I know except in certain cases like ordering by date (since the ObjectId's have the datetime in them, etc.)

For example you'd lose the ability to simply order by the _id you'd also lose the benefits for sharding and distribution. Aside from that, while I'd still personally use the ObjectId's anyhow ... as long as the int is unquie (of course) ... you should be just fine.

Since the _id always "comes back" in a query I suppose you'd save a little time and data transfer (a bitty bit.)

You can even make your _id an array if you wanted, and it'll all index nicely see this answer (not that I'd necessarily recommend that most of the time.)

Also see: Optimizing Object IDs



来源:https://stackoverflow.com/questions/4637061/facebook-user-id-as-mongodb-bson-objectid

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