Querying Members

The queryMembers endpoint allows you to list and paginate members for a channel. The endpoint supports filtering on numerous criteria to efficiently return member information. This endpoint is useful for channels that have large lists of members and you want to search members or if you want to display the full list of members for a channel.

Pagination and ordering

By default members are ordered from oldest to newest and can be paginated using offset-based pagination or by created_at or user_id fields.

Pagination by offset is the simplest to implement but it can lead to incorrect results if the list of members changes while you are paginating.

The recommended approach is to sort by “created_at” or by “user_id”.

// Android SDK

ChannelClient channelClient = client.channel("messaging", "general");
int offset = 0;
int limit = 10;
FilterObject filterByName = Filters.neutral();

// paginate by user_id in descending order
QuerySorter<Member> sort = QuerySortByField.descByName("userId");
channelClient.queryMembers(offset, limit, filterByName, sort, emptyList()).enqueue(result -> {
  if (result.isSuccess()) {
    List<Member> members = result.data();
  } else {
    // Handle result.error()
  }
});

// paginate by created at in ascending order
QuerySorter<Member> createdAtSort = QuerySortByField.ascByName("createdAt");
channelClient.queryMembers(offset, limit, filterByName, createdAtSort, emptyList()).enqueue(result -> {
  if (result.isSuccess()) {
    List<Member> members = result.data();
  } else {
    // Handle result.error()
  }
 });
 

// Backend SDK

// paginate by user_id in descending order
ChannelQueryMembersResponse members = Channel.queryMembers()
  .sort(Sort.builder().field("user_id").direction(Direction.DESC).build())
  .limit(10)
  .request();
  
// paginate by created at in ascending order
ChannelQueryMembersResponse members = Channel.queryMembers()
  .sort(Sort.builder().field("created_at").direction(Direction.ASC).build())
  .limit(10)
  .request();

Stream Chat does not run MongoDB on the backend, only a subset of the query options are available.

Here’s some example of how you can query the list of members:

// Android SDK

ChannelClient channelClient = client.channel("messaging", "general");

int offset = 0; // Use this value for pagination
int limit = 10;
QuerySortByField<Member> sort = new QuerySortByField<>();

// Channel members can be queried with various filters
// 1. Create the filter, e.g query members by user name
FilterObject filterByName = Filters.eq("name", "tommaso");
// 2. Call queryMembers with that filter
channelClient.queryMembers(offset, limit, filterByName, sort, emptyList()).enqueue(result -> {
  if (result.isSuccess()) {
    List<Member> members = result.data();
  } else {
    // Handle result.error()
  }
});

// Here are some other commons filters you can use:

// Autocomplete members by user name (names containing "tom")
FilterObject filterByAutoCompleteName = Filters.autocomplete("name", "tom");

// Query member by id
FilterObject filterById = Filters.eq("id", "tommaso");

// Query multiple members by id
FilterObject filterByIds = Filters.in("id", Arrays.asList("tommaso", "thierry"));

// Query channel moderators
FilterObject filterByModerator = Filters.eq("is_moderator", true);

// Query for banned members in channel
FilterObject filterByBannedMembers = Filters.eq("banned", true);

// Query members with pending invites
FilterObject filterByPendingInvite = Filters.eq("invite", "pending");

// Query all the members
FilterObject filterByNone = NeutralFilterObject.INSTANCE;

// We can order the results too with QuerySortByField param
// Here example to order results by member created at descending
QuerySortByField<Member> createdAtDescendingSort = QuerySortByField.descByName("createdAt");

// Backend SDK
// Channel members can be queried with various filters
// 1. Create the filter, e.g query members by user name
Map<String, Object> filterById = FilterCondition.eq("name", "tommaso");

// 2. Call queryMembers with that filter
ChannelQueryMembersResponse members = Channel.queryMembers()
 .filterConditions(filterByName)
 .request();

// Here are some other commons filters you can use:

// Autocomplete members by user name (names containing "tom")
Map<String, Object> filterByAutoCompleteName = FilterCondition.autocomplete("name", "tom");

// Query member by id
Map<String, Object> filterById = FilterCondition.eq("id", "tommaso");

// Query multiple members by id
Map<String, Object> filterByIds = FilterCondition.in("id", Arrays.asList("tommaso", "thierry"));

// Query channel moderators
Map<String, Object> filterByModerator = FilterCondition.eq("is_moderator", true);

// Query for banned members in channel
Map<String, Object> filterByBannedMembers = FilterCondition.eq("banned", true);

// Query members with pending invites
Map<String, Object> filterByPendingInvite = FilterCondition.eq("invite", "pending");

// We can order the results too with Sort param
// Here example to order results by member created at descending
Sort createdAtDescendingSort = Sort
 .builder()
 .field("created_at")
 .direction(Sort.Direction.DESC)
 .build();

Query Parameters

nametypedescriptiondefaultoptional
filtersobjectThe query filters to use. You can query on any of the custom fields defined above{}
sortobjectthe sort parameters{ created_at:1}
optionsobjectpagination options{ limit:100, offset:0}

By default when query members does not have any filter and it will match all members on your channel.

Member Queryable Built-In Fields

The following fields can be used to filter your query results

NameTypeDescriptionsupported operatorsExample
idstringthe id of the user$eq $intom
namestringthe name of the user$eq, $in, $autocomplete, $qTommaso
channel_rolestringthe member role$eqchannel_moderator
bannedbooleanthe banned status$eqfalse
invitestring, must be one of these values: (pending, accepted, rejected)the status of the invite$eqpending
joinedbooleanwhether member is joined the channel or not$eqtrue
created_atstring, must be formatted as an RFC3339 timestampthe time that the member was created$eq, $gt, $gte, $lt, $lte2021-01-15T09:30:20.45Z
updated_atstring, must be formatted as an RFC3339 timestampthe time the member was last updated$eq, $gt, $gte, $lt, $lte2021-01-15T09:30:20.45Z
last_activestring, must be formatted as an RFC3339 timestampthe time the user was last active$eq, $gt, $gte, $lt, $lte2021-01-15T09:30:20.45Z
cidstringthe cid of the channel that the user is a member of$eqmessaging:general
user.emailstringthe ‘email’ property of the user$eq, $in, $autcompleteuser@example.com

Also, you can pass any field available in the custom data.

Query Options

nametypedescriptiondefaultoptional
limitintegerThe number of members to return (max is 100)100
offsetintegerThe offset (max is 1000)0
user_id_ltstringPagination option: excludes members with ID greater or equal the value-
user_id_ltestringPagination option: excludes members with ID greater than the value-
user_id_gtstringPagination option: excludes members with ID less or equal the value-
user_id_gtestringPagination option: excludes members with ID less than the value-
created_at_afterstringPagination option: select members created after the date (RFC399)-
created_at_beforestringPagination option: select members created before the date (RFC399)-
created_at_before_or_equalstringPagination option: select members created before or equal the date (RFC399)-
created_at_after_or_equalstringPagination option: select members created after or equal the date (RFC399)-

Response

Field NameDescription
membersThe list of members matching the query
© Getstream.io, Inc. All Rights Reserved.