开发者

MongoDB Schema Design, Coalesce, Merging user-specific fields with default fields

Given:

A database of objects that each have a default weighting for search order (perhaps a universal popularity rating).

As each user uses those objects, the user's personal weighting for search order is stored for each item (using whatever algorithm, such as frequency of use, etc.).

In SQL this is pretty easy to do by joining the objects and user_objects tables and doing something along the lines of

select ... coalesce(user_objects.personal_weighting, objects.default_weighting) 
    as sort_key order_by sort_key

In other words, the user can search the entire DB of objects (let's say songs for example). If there's an object that the user 开发者_JAVA百科has never interacted with before (a song they have never listened to), then the search order weighting for that object is based on a default stored for each object. If the user has interacted with an object, then that object's search order weighting is based on the specific user's weighting, over-riding the default.

Is there any efficient way to model this in MongoDB? It would be fairly easy and performant using map/reduce in CouchDB because of the indexes stored for the map/reduced views, but I haven't figured out how to do this well in MongoDB.

Any ideas?


I think the only way you can make this scalable is the following:

Create a seperate collection for each user which contains only the weight for the visited objects. Add an index. Then retrieve the top weighted ObjectIds+weight from this collection. Get the ObjectIds+weights for the top weighted elements from your original collection. Merge these two lists and finally retrieve the needed elements.

All the queries are indexed so this should be fast. The pagination code for the result set is more complex (merging step) and won't work fast if your users want to check beyond.. let's say the 50th page or so (just disable it, like google does).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜