Querying Users

The Query Users method allows you to search for users and see if they are online/offline. The example below shows how you can retrieve the details for 3 users in one API call:

var resp = await userClient.QueryAsync(QueryUserOptions.Default.WithFilter(new Dictionary<string, object>
{
  { "id", new Dictionary<string, object> { { "$in", new[]{ "john", "jack", "jassie" } } } },
}));
var query = QueryUserOptions.Default.WithFilter(new Dictionary<string, object>
{
  { "id", new Dictionary<string, object> { { "$in", new[]{"jessica"} } } },
});
query.Presence = true;

var resp = await userClient.QueryAsync(query);

Another option is to query for banned users. This can be done with the following code snippet:

var resp = await userClient.QueryAsync(QueryUserOptions.Default.WithFilter(new Dictionary<string, object>
{
  { "id", new Dictionary<string, object> { { "$in", new[]{ "john" } } } },
  { "banned": true },
}));

An object with an array of users will be returned.

Please be aware that this query will return users banned across the entire app, not at a channel level.

All filters use a Mongoose style syntax; however, we do not run MongoDB on the backend, so you can only use a subset of queries that are normally available within Mongoose. The supported queries are described below.

You can filter and sort on the custom fields you’ve set for your user, the user id, and when the user was last active.

Supported queries

The options for the queryUser method are include_deactivated_users, presence, limit, and offset. If presence is true this makes sure you receive the user.presence.changed event when a user goes online or offline.

nametypedescriptiondefaultoptional
filter_conditionsobjectsConditions to use to filter the users-
presencebooleanGet updates when the user goes offline/onlinetrue
sortobject or array of objectsThe sorting used for the users matching the filters. Sorting is based on field and direction, and multiple sorting options can be provided. Direction can be ascending (1) or descending (-1).[{created_at: -1}, {id: -1}]
limitintegerNumber of users to return30
offsetintegerOffset for pagination0
id_gtstringID-based pagination. Return IDs greater than this ID. If this is not empty, the default sort order will be [{id: -1}].-
id_gtestringID-based pagination. Return IDs greater than or equal to this ID. If this is not empty, the default sort order will be [{id: -1}].-
id_ltstringID-based pagination. Return IDs less than this ID. If this is not empty, the default sort order will be [{id: -1}].-
id_ltestringID-based pagination. Return IDs less than or equal to this ID. If this is not empty, the default sort order will be [{id: -1}].-
include_deactivated_usersbooleanInclude deactivated users in the response-

You can subscribe to presence status of at most 30 users using this method.

Note - The offset limit is set to 1000.

Filter conditions

NameTypeDescriptionallowed operators
idstringID of the user$eq, $gt, $gte, $lt, $lte, $in, $autocomplete
rolestringrole of the user$eq, $gt, $gte, $lt, $lte, $in
bannedbooleanusers that have been banned$eq
shadow_bannedbooleanusers that have been shadow banned$eq
created_atstring, must be formatted as an RFC3339 timestampcreated time of the user$eq, $gt, $gte, $lt, $lte, $in
updated_atstring, must be formatted as an RFC3339 timestampupdated time of the user$eq, $gt, $gte, $lt, $lte, $in
last_activestring, must be formatted as an RFC3339 timestamptime when the user was last active$eq, $gt, $gte, $lt, $lte, $in
teamsstringteams that the user belongs to$eq, $contains
namestringname property of the user$eq, $autocomplete
usernamestringusername property of the user$eq, $autocomplete

Querying Using the $autocomplete Operator

You can autocomplete the results of your user query by username and/or ID.

1. By Name

If you want to return all users whose field 'name' has a value that includes 'ro', you could do so with the following:

var resp = await _userClient.QueryAsync(QueryUserOptions.Default.WithFilter(new Dictionary<string, object>
{
  { "name", new Dictionary<string, object> { { "$autocomplete", "ro" } } },
}));

This would return an array of any matching users, such as:

[
  {
    "id": "userID",
    "name": "Curiosity Rover"
  },
  {
    "id": "userID2",
    "name": "Roxy"
  },
  {
    "id": "userID3",
    "name": "Roxanne"
  }
]

2. By ID

var resp = await _userClient.QueryAsync(QueryUserOptions.Default.WithFilter(new Dictionary<string, object>
{
  { "name", new Dictionary<string, object> { { "$autocomplete", userId } } },
}));
© Getstream.io, Inc. All Rights Reserved.