Mongodb, simple IN problem

Posted by afvasd on Stack Overflow See other posts from Stack Overflow or by afvasd
Published on 2010-05-24T07:14:26Z Indexed on 2010/05/24 10:51 UTC
Read the original article Hit count: 149

Filed under:

Hi everyone I am new to mongo, this is my db design:

product := { 
    name: str
    group: ref,
    comments: [ ref, ref, ref, ref ] 
}

comments := { 
... a bunch of comments stuff

} 

tag := { 
    _id: int,               #Need this for online requests
    tag: str,
    products: [ {product: ref, score: float}, ... ],
    comments: [ {comment: ref, score: float}, ...],
}

So my usage pattern is: GIVEN a product, find comments that have certain tag and sort them accordingly.

My current approach involves:

  1. Look for that tag object that has tag=myTag
  2. pull all the comments out, sorted
  3. look for that product where product.name=myProduct
  4. pull all the comments out (which are dbrefs by the way)
  5. loop through the result of 2, and checking if they are in 4, (this I can do a limit 10) etc.

It's pretty inefficient. Any better methods?

© Stack Overflow or respective owner

Related posts about mongodb