0

I have a friend list (list of object ids), and a search query. I want to search my friends whose first name is starting like the search query. I can see I need to use map/reduce, but I don't think of how to do it.

class UserAccount(Document):
    first_name = StringField(max_length = 20)
    last_name = StringField(max_length = 20)
    user_name = StringField(max_length = 20)
    email = EmailField()
    password = StringField(max_length = 20)
    friends = ListField(ReferenceField('self'))


# List Of Friends IDs
friends = [ObjectId("5278a1821d41c80d7a3ed4ec"), ObjectId("5278a1821d41c80d7a3ed4fe"), 
ObjectId("5278a1821d41c80d7a3ed4aa"), ObjectId("5278a1821d41c80d7a3ed4bb")]    

# Assume a search query. 
search_query = 'some-name'

regex = re.compile('^' + re.escape(search_query))
users = UserAccount._get_collection()
result = users.find( 
{ 
    "_id" : friends[0], 
    "first_name" : regex                    
})

Any help or references are appreciated?

4

1 回答 1

1

不需要 map reduce - 这应该找到所有匹配的朋友:

UserAccount.objects.filter(pk__in=friends, name=regex)
于 2013-11-05T11:09:16.450 回答