Querying Members

The queryMembers method allows you to list and paginate members for a channel. It supports filtering on numerous criteria to efficiently return member information. This is useful for channels with large member lists where you need to search for specific members or display the complete member roster.

// Query members by user name
await channelClient.QueryMembersAsync(new QueryMembersRequest
{
  FilterConditions = new Dictionary<string, object> { { "name", "tommaso" } },
});

// Autocomplete members by user name
await channelClient.QueryMembersAsync(new QueryMembersRequest
{
  FilterConditions = new Dictionary<string, object>
  {
    { "name", new Dictionary<string, string> { { "$autocomplete", "tom" } } },
  },
});

// Query all members
await channelClient.QueryMembersAsync(new QueryMembersRequest
{
  FilterConditions = new Dictionary<string, object>(),
});

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

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 queryMembers is called without any filter, it matches all members in the 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 the member has joined the channel$eqtrue
created_atstring, must be formatted as an RFC3339 timestampThe time 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 the user is a member of$eqmessaging:general
user.emailstringThe email property of the user$eq, $in, $autocompleteuser@example.com

You can also filter by any field available in the custom data.

Paginating Channel Members

By default, members are ordered from oldest to newest. You can paginate results using offset-based pagination or by the created_at or user_id fields.

Offset-based pagination is the simplest to implement, but it can lead to incorrect results if the member list changes while you are paginating. The recommended approach is to sort by created_at or user_id.

// Paginate by user_id in descending order
await channelClient.QueryMembersAsync(new QueryMembersRequest
{
  FilterConditions = new Dictionary<string, object>(),
  Sorts = new[] { new SortParameter { Field = "user_id", Direction = SortDirection.Descending } },
  Limit = 10,
  Offset = 0,
});

// Paginate by created_at in ascending order
await channelClient.QueryMembersAsync(new QueryMembersRequest
{
  FilterConditions = new Dictionary<string, object>(),
  Sorts = new[] { new SortParameter { Field = "created_at", Direction = SortDirection.Ascending } },
  Limit = 10,
  Offset = 0,
});

Pagination 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 than or equal to the value-
user_id_ltestringPagination option: excludes members with ID greater than the value-
user_id_gtstringPagination option: excludes members with ID less than or equal to the value-
user_id_gtestringPagination option: excludes members with ID less than the value-
created_at_afterstringPagination option: select members created after the date (RFC3339)-
created_at_beforestringPagination option: select members created before the date (RFC3339)-
created_at_before_or_equalstringPagination option: select members created before or equal to the date (RFC3339)-
created_at_after_or_equalstringPagination option: select members created after or equal to the date (RFC3339)-