mongoengine在EmbeddedDocument ListField中搜索

时间:2014-04-15 15:46:16

标签: django mongodb mongoengine

我使用django-mongonengine来使用mongdb数据库;我有一个关于在这样的结构中过滤/搜索的问题:

class Shop(Document):
created = DateTimeField(default=datetime.now,verbose_name='date_created')
updated = DateTimeField(default=datetime.now,verbose_name='date_updated')
code = StringField(required=True,unique=True)
socialName = StringField(verbose_name='legal_name')
name = StringField(required=True,verbose_name='shop_name')
address = StringField(required=True,verbose_name='shop_address')
gpsLoc = GeoPointField()
apdo = IntField(default=0)
logo = StringField(verbose_name='url_logo')
listGifts = ListField(EmbeddedDocumentField(Gift))
listOffers = ListField(EmbeddedDocumentField(Offer))
def __unicode__(self):
    return u'%s' % (self.name)
@queryset_manager
def objects(doc_cls,queryset):
    return queryset.order_by('-shop_name')


class Gifted(EmbeddedDocument):
    created = DateTimeField(default=datetime.now,verbose_name='date_created')
    gifted = DateTimeField(default=datetime.now,verbose_name='date_gifted')
    shop = ReferenceField(Shop,required=True,verbose_name='shop')
    gift = EmbeddedDocumentField(Gift,required=True,verbose_name='gift')
    def __unicode__(self):
        return u'%s' % (self.gift)
    @queryset_manager
    def objects(doc_cls,queryset):
        return queryset.order_by('-date_gifted')




class UserProfile(EmbeddedDocument):
    created = DateTimeField(default=datetime.now,verbose_name='date_created')
    updated = DateTimeField(default=datetime.now,verbose_name='date_updated')
    birthday = DateTimeField(verbose_name='birthday')
    gender = BooleanField(default=True,verbose_name='is_man')
    rel_status = StringField()
    name = StringField(max_length=50,verbose_name='name')
    phone = StringField(verbose_name='phone')
    points = IntField(default=0,verbose_name='current_points')
    listGifts = ListField(EmbeddedDocumentField(Gifted))
    listOffers = ListField(EmbeddedDocumentField(Discount))
    def __unicode__(self):      return self.name
    @queryset_manager
    def objects(doc_cls,queryset):
        return queryset.order_by('update')



class User(Document):
    username = EmailField(unique=True,max_length=250,min_length=4,required=True)
    password = StringField(max_length=100,required=True)
    created = DateTimeField(default=datetime.now,verbose_name='date_created')
    code = StringField(required=True,unique=True)
    tok_crypt = StringField(verbose_name='token_crypt',required=True)
    expired = DateTimeField(verbose_name='date_expired')
    last_login = DateTimeField(default=datetime.now, verbose_name='date_last_login')
    profile = EmbeddedDocumentField(UserProfile, verbose_name='profile')
    def __unicode__(self):  return self.username
    @queryset_manager
    def objects(doc_cls,queryset):
        return queryset.order_by('username')

如果我想从特定商店(Shop.code)搜索所有具有一些天赋物品的用户,我怎样才能过滤/搜索而不使用内部,内部等...

类似的东西:

  

listUsers = User.objects(profile.listGifts__contains = Gifted.objects(shop.code = sCode))

listUsers = User-> Profile-> List(Gifted-> Shop.code = code)

1 个答案:

答案 0 :(得分:1)

首先,你必须获得具有给定代码的商店:

shop = Shop.objects(code=code)[0]

然后使用它在用户集合中搜索:

listUsers = User.objects(profile__listGifts__shop=shop)

用于在列表中搜索的MongoEngine语法非常简单:您只需编写查询,就像listGifts不是列表一样,它将查询列表listGifts包含给定商店的所有用户。

MongoEngine不做的是加入。我的意思是你不能对一个以上的集合进行单一查询。上面的第一个查询属于Shop类集合。 User类集合的第二个 - UserProfileGifted是同一集合中的嵌入文档。