I am trying to achieve the best possible way to search between different collections.
Here is a simple scenario:
Just like WhatsApp, I want to search the chats.
Here is a very simple criteria I just created:
I have 2 collections:
Users:
[
{
"_id": "Xuibgsadbgsi35Gsdf",
"fullName": "User A"
},
{
"_id": "Afstg34tg4536gh",
"fullName": "User B"
},
{
"_id": "KHJDFhfs7dfgsvdfwsef",
"fullName": "User C"
}
]
Chatting:
[
{
"_id": "Phsdfyg92345sgb7651",
"title": "Group 1",
"chatType": "groupChatting"
},
{
"_id": "YONgsa793423bD",
"title": "All users group",
"chatType": "groupChatting"
},
{
"_id": "Uysdf345GBHsdf",
"chatType": "oneToOneChatting",
"ownerUserId": "Afstg34tg4536gh",
"chatWithUserId" "Xuibgsadbgsi35Gsdf"
},
{
"_id": "Fbgu283gbnap023656",
"chatType": "oneToOneChatting",
"ownerUserId": "KHJDFhfs7dfgsvdfwsef",
"chatWithUserId" "Afstg34tg4536gh"
},
{
"_id": "Ong7gAUDSFg7235ng",
"title": "Group 2",
"chatType": "groupChatting"
}
]
Here is my query:
Chatting.find({"title": {$regex : ".*user.*", $options: 'i' }}).fetch();
Now the expected result here is:
[
{
"_id": "YONgsa793423bD",
"title": "All users group",
"chatType": "groupChatting"
},
{
"_id": "Uysdf345GBHsdf",
"chatType": "oneToOneChatting",
"ownerUserId": "Afstg34tg4536gh",
"chatWithUserId" "Xuibgsadbgsi35Gsdf"
},
{
"_id": "Fbgu283gbnap023656",
"chatType": "oneToOneChatting",
"ownerUserId": "KHJDFhfs7dfgsvdfwsef",
"chatWithUserId" "Afstg34tg4536gh"
}
]
Now here you can see that I wont get any results, because I have Users in other collections but technically from App User perspective, he should see the chats:
- Uysdf345GBHsdf
- Fbgu283gbnap023656
Now in my mind I can do something like, I first search those users with searchText: User
, and then collect all those _id
from Users
collection in array and match in Chatting
collection, but this seems way too bad...
So can anyone tell me what is the best possible way to achieve such scenario?