At the core of many applications is the ability to retrieve a list of channels that are accessible by the user. However, due to the wide array of use cases, the queryChannels endpoint in the Stream Chat API is very flexible and can be used to fit almost any use case.
Here are some of the examples that could help to apply operators on filters.
QueryChannels
const channel = await client.queryChannels(filter, sort, {
watch: true,
state: true,
});
Default filter
By default, queryChannels / queryMembers/ queryUsers will return all values if there are no filters applied.
Minimum filter
This is the minimum recommended filter when fetching channels. It will return the channels of which the user is a member of.
const filter = { members: { $in: [userID] } }
Filter for non-empty channels using the less than operator
Often you are are going to want to remove any channels that are empty. In order to do that, you can use the last_message_at field, in addition to the $lt (or $lte) which is less than (or less than or equal).
const filter ={
type:"messaging",
members:{$in: ["userID"] },
last_message_at:{$lt:dateInThePast},
};
*dateInThePast can be the current-time
Filter for hidden channels using or operator
const filter = {
$or: [
{
members: {
$in: [userID],
},
},
{
hidden: true,
},
]
}
Filter for both members and others with $and
This will return all messaging type channels that have both George and Steve as members but could have other members as well
const bothMembers = {
$and: [
{ type: "messaging" },
{
members: {
$in: ["steve"],
},
},
{
members: {
$in: ["george"],
},
},
],
};
Filter for only two members with $eq
Unlike the above filter, this will return only messaging channels with Steve and George as members
const filter = {
type: "messaging",
members: { $eq: ["steve", "george"] },
};
Filter using $in operator
This query will return any and all channels with a cid contained in the given array
const filter = {
cid: {
$in: ['messaging:awesome-chat', 'messaging:Firstchannel']
}
}
Filter for pending invites with $and
The invite field is a special field that concerns our invite system. You can query for channels that have a pending, accepted, or rejected status for a user.
const filter = {
$and: [
{ type: "messaging" },
{
members: {
$in: [userID],
},
},
{
invite: "pending",
},
],
};
Filters Based On Members
- Filter for channels in which 'userID1' is a member
const filter = { members: { $in: ['userID1'] }};
- Filter for channels where 'userID1' or 'userID2' are a member
const filter = { members: { $in: ['userID1', 'userID2'] }};
- Filter for channels where 'userID1' and 'userID2' are both a member
const filter = { members: { $in: ['userID1']} && { $in: ['userID2'] }};
How To Filter Out Channels Without Messages From Your Channel Search
You may be wondering how to query for channels with messages and exclude all channels that are without messages. To do this, simply follow the steps below.
1. First, query for channels as you normally would and as explained in our documentation. For example, use the following code snippet taken from this page in the docs.
const filter = { members: { $in: ['thierry'] } };
const sort = { last_message_at: -1 };
const channels = await chatClient.queryChannels(filter, sort, {
watch: true,
state: true,
});
2. Next, add the following parameter: "last_message_at" into the filter variable. Set "last_message_at" to a time in that past that will encapsulate all potential times a message could have been sent in your app. This will return any channels that have messages and exclude all channels without messages.
const filter = {
type: "messaging",
member: { $in: ['thierry'] },
last_message_at: { $gt: "2020-10-10T16:01:49.27673Z" },
}
How to test if a user is a member of a channel already
Testing if a member is already a member of a channel before taking further action is fairly common practice in most chat applications. Here is an example of how you can check
const filter = { id: channelID, members: { $in: [userID] } };
const queryChannelMember = async () => {
return await chatClient.queryChannels(filter, sort);
};
queryChannelMember()
.then( channels => channels.length ? callbackTrue() : callbackFalse() )
.catch( err => console.log(err) );
Query channels by: created_by user / Not created_by user
To query channels for channels which the user has created
const filter = { created_by_id: user_id };
To search for channels of which you are a member AND did not create, this is the filter
const filter = {
members: { $in: [userID] },
created_by_id: { "$nin": [userID] }
};
Query all conversations from Channel Types: 'messaging', 'livestream', & 'gaming'
- And exclude empty conversations from Channel Type: gaming
const filter = {
members: { $in: [userId] },
$or: [
{
type: {
$in: ["messaging", "livestream"],
},
},
{
type: "gaming",
last_message_at: {
$gt: "2021-01-01T01:01:01.000000Z",
},
},
],
};
Comments
6 comments
We really need contains/regex operator for queries. Is there any chance that you will add them?
Hello! Thanks for leaving a comment! There is no plan to include regex in our filters though - they are just too slow.
And what about the contains operator instead of the regex?
Hi, can you please send us your use case to https://getstream.io/contact/support/? Thanks.
Hello!
Filter for only two members with $eq
Filter for channels where 'userID1' or 'userID2' are a member
---
These two paragraphs use different syntax. Do they have the same result? Using $eq in my code does not return any Channel to me
@Seetha, can the $in filter be used right one with custom meta data keys ?
For example using node sdk
Please sign in to leave a comment.