Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

node.js - Meteor Mongodb deep searching

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
  • Chattings

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...