Я пытаюсь отфильтровать встроенные документы по строке (как запрос * SQL эквивалент) из родительского документа.
Вот что у меня сейчас:
def resolve_friend(root, info, **kwargs):
phone = kwargs.get('phone', None)
search_string = kwargs.get('searchString', None)
try:
// I am retrieving the parent doucment by phone number first
account = AccountModel.objects(phone=phone).first()
if account is None:
return ResponseMessageField(is_success=False, message="User does not exists.")
top_friends = None
// Now I want to filter the Friends field from the account by the search string.
// In the friends' field, there are multiple documents. The idea is to get the friends
// matching the string from their first_name / last_name
if search_string:
regex = re.compile('.*' + search_string + '.*', re.IGNORECASE)
//doesn't work any of these below
# top_friends = account.friends.filter((Q(first_name='test') | Q(last_name='test')))
top_friends = AccountModel.objects(friends__first_name__icontains='tes').all()
all_friends = account.friends.filter(request_status="ACCEPTED") //this works fine
return retrieve_friend_list_graphql(top_friends=top_friends, all_friends=all_friends)
except Exception as e:
return ResponseMessageField(is_success=False, message="Account fetch error. " + str(e))
Эти строки не
top_friends = account.friends.filter((Q(first_name='test') | Q(last_name='test')))
top_friends = AccountModel.objects(friends__first_name__icontains='tes').all()
Я не могу отфильтровать account.friends.filter()
по строке поиска в mongoengine.
Вот полный документ account
с полем friends
.
{
"_id": {
"$oid": "5e3485c2dd26ea191faf2861"
},
"identity": "0b1170fc50979464c352c34c2e9fd7aa",
"username": "Shuvro",
"first_name": "",
"middle_name": "",
"last_name": "",
"phone": "+8801521483999",
"is_active": false,
"is_activation_sent": true,
"activation_code": "9815",
"is_phone_confirmed": false,
"is_email_active": false,
"is_email_activation_sent": true,
"email_activation_code": "7529c11c2b0eb55314fb01355d5cc78f",
"is_email_confirmed": false,
"friends": [
{
"identity": "bb30337b7fbcc5080b9fe3a2b6a60383",
"username": "Shuvro",
"first_name": "lord",
"middle_name": "",
"last_name": "",
"email": "shuvro@test.com",
"phone": "+8801521483720",
"request_status": "PENDING",
"request_type": "RECEIVED",
"request_created_at": {
"$date": {
"$numberLong": "1580676652938"
}
}
},
{
"identity": "06df8a2dd85c3ebc5b49817333f2aeff",
"username": "",
"first_name": "test",
"middle_name": "",
"last_name": "test last",
"phone": "+8801521474747",
"request_status": "PENDING",
"request_type": "RECEIVED",
"request_created_at": {
"$date": {
"$numberLong": "1581876704806"
}
}
}
]
}
Пожалуйста, помогите, как я могу отфильтровать друзей по их имени из родительского документа. Благодаря.