Quantcast
Channel: Sullerton
Viewing all articles
Browse latest Browse all 12

Advanced Queries with mongoDB and mongoengine

$
0
0

I have been using mongoengine for the past year and life’s been a breeze. This week, however, I hit the first time that I needed an advanced query that couldn’t be expressed with the basic mongoengine api. It was time to go raw dog, and there’s no turning back. Mongoengine probably suffices in most situations, but sometimes you need a little more control over your query.

Lets say I have the following mongoengine Document:

from mongoengine import *
import User #another mongongine Document
import Team #another mongongine Document
 
class Item(Document):
    team = ReferenceField(Team)
    creator = ReferenceField(User)
    public = BooleanField(default=False)

Users of our system belong to a Team, and they create Items. A User should only have access to Items that he/she created or that are on the same Team and public.

So lets query for a list of Item’s a User has access to!

I had originally tried to do this with mongoengine’s advanced queries, but I had horrible results. After a full day of messing around with it I can not explain why the results were coming back as they were.

It was time to understand a little more about mongoDB advanced queries, and I feel like a better man for it. So I wanted to find all the Items that are part of a particular Team, that are either public or created by the logged in user:

....
user = logged_in_user
team = user.team
 
items_query = Item.objects(__raw__={ 'team.$id' :team.id, '$or': [ { 'public' : True }, { 'creator.$id' : user.id } ] })
...

Took me a while to figure out what property mongoengine was storing the referenced object id as ($id) and figured it would be of help to someone else. Thanks to apiguy for that!

I get the best of both worlds, the fine tooth comb that the raw query provides and the result comes back wrapped up as my mongoengine Documents.


Viewing all articles
Browse latest Browse all 12

Trending Articles