1

For a Blog webapp, I am trying to model a set of users, blogs and actions on resources in mongo. Users can perform a no of actions such as 'like', 'star', 'feature' resources of a blog post such as 'blog posts' ,'links', 'images', etc.

The model will look like

User(user_id: long) Resource(resource_id: long) Resource/Action(int)

The first 16 bits are used to store actions and the next 15 for identifying what type of resource it is.

So given a query, - find the list of users who have liked an blog post.

What would be the best way to model and query this in mongo ?

I thought of something like

User(user_id: long) Resource(resource_id: long) Resource/Action(int)
2421423                 4325235234                 17
4223545                 3454235432                 18
4235234                 4343453425                 17

and using bitmask AND operations on the query parameters and the Resource/Action field to filter correct records. But looks like mongo does not support bitwise operations. I read $where javascript functions could be used for this purpose - but I am concerned about the performance of using js function calls for querying.

What would be a good way to achieve this use case ?

Thanks!

Skynet
  • 657
  • 2
  • 9
  • 25
  • Are you sure? I don't know mongo but [this question](http://stackoverflow.com/questions/7257896/setting-individual-bits-in-mongo-to-store-a-bitmask) seems to imply some kind of binary operation support. – Tim Sep 20 '12 at 21:16
  • 1
    Unfortunately, $bit is only available for updates, not for queries. – Philipp Sep 20 '12 at 23:21
  • 2
    Phillip is correct as at MongoDB 2.2.0. The relevant Jira issue to watch/vote on for `$bit` query support appears to be: [SERVER-3518](https://jira.mongodb.org/browse/SERVER-3518). Your best modelling approach at the moment would be to use separate fields instead of bitwise queries. Using server-side JavaScript such as $where could be [significantly slower](http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-%7B%7B%24where%7D%7DClausesandFunctionsinQueries). – Stennie Sep 21 '12 at 12:20

0 Answers0